Book Image

Learning Scala Programming

By : Vikash Sharma
Book Image

Learning Scala Programming

By: Vikash Sharma

Overview of this book

Scala is a general-purpose programming language that supports both functional and object-oriented programming paradigms. Due to its concise design and versatility, Scala's applications have been extended to a wide variety of fields such as data science and cluster computing. You will learn to write highly scalable, concurrent, and testable programs to meet everyday software requirements. We will begin by understanding the language basics, syntax, core data types, literals, variables, and more. From here you will be introduced to data structures with Scala and you will learn to work with higher-order functions. Scala's powerful collections framework will help you get the best out of immutable data structures and utilize them effectively. You will then be introduced to concepts such as pattern matching, case classes, and functional programming features. From here, you will learn to work with Scala's object-oriented features. Going forward, you will learn about asynchronous and reactive programming with Scala, where you will be introduced to the Akka framework. Finally, you will learn the interoperability of Scala and Java. After reading this book, you'll be well versed with this language and its features, and you will be able to write scalable, concurrent, and reactive programs in Scala.
Table of Contents (21 chapters)
Title Page
Packt Upsell
Contributors
Preface
Index

Introduction to Scala


Consider a scenario where you get a paragraph and a word and you are asked to get the number of occurrences for that word. You're lucky enough to know a language such as Java. Your solution might look like this:

String str = "Scala is a multi-paradigm language. Scala is scalable too."
int count = 0;
for (stringy: str.split (" ")) {
    if (word.equals (stringy))
        count++;
}
System.out.println ("Word" + word + " occurred " + count + " times.")

That was easy, wasn't it? Now our Scalable language has a simple way of accomplishing this. Let's take a look at that:

val str = "Scala is a multi-paradigm language. Scala is scalable too."
println ("Word" + word + " occurred " + str.split(" ").filter(_ == word).size + " times.")

That's it, a one-liner solution for the same problem. The code may not look familiar right now, but gradually you'll have command over it. By the end of this chapter, we'll understand everything that's needed to run a Scala program, not just a Hello World program, but one that does something.

Scala's no different. It runs on JavaVirtualMachine (JVM), so Java folks must have an idea about it. If not, JVM is defined as an abstract computing machine that operates on a set of instructions (Java Bytecode). It enables a machine to run a Java program. So here's the conclusion: when we write Scala programs and compile them, they are converted into Java Bytecode and then run on JVM. Scala interoperates with all Java libraries. It's easier and, of course, possible to write our own Scala code and also incorporate library functions written in Java.

Scala is a multi-paradigm language; it's a mixture of object-oriented and functional programming. But what good is it to us?

A programming paradigm

A paradigm is simply a way of doing something. So a programming paradigm means a way of programming or a certain pattern of writing programs. There are a number of programming paradigms in existence, but four of them have gained popularity:

  • Imperative Paradigm: First do this and then do that
  • Functional Paradigm: Evaluate and use
  • Logical Paradigm: Answer through solution
  • Object-Oriented Paradigm: Send messages between objects to simulate temporal evolution of a set of real-world phenomena

Object-oriented versus functional paradigms 

With its roots in the mathematics discipline, the functional programming paradigm is simple. It works on the theory of functions which produce values that are immutable. Immutable values mean they can't be modified later on directly. In the functional paradigm, all computations are performed by calling self/other functions. Functions are first-class citizens in the functional world. This opens up a new world of possibilities where all computations are driven by a certain need.

The object-oriented planet revolves around encapsulation and abstractions. The logical grouping of components makes maintenance of larger and complex programs easier. Data and models are encapsulated in objects. Information hiding is effective for containing an object's properties. Inheritance hierarchies, the concept of classes, and messaging between objects makes the whole model/pattern of object-oriented programming a partial success.

Scala is multi-paradigm

Scala, being a multi-paradigm language, supports both paradigms. As we're learning Scala, we have the power of both of these paradigms. We can create functions as we need them, and also have objects talking to other objects. We can have class hierarchies and abstractions. With this, dominance over a particular paradigm will not affect another.

Today the need for concurrency, immutability, heterogeneity, reactiveness, and fault tolerant architectures with ever-shrinking development life cycles has drastically increased. In this era, languages such as Scala do more than they need to with their support for functional as well as object-oriented programming.

For a programmer like us, a language is a tool to create something meaningful. We tend to reuse and manipulate other tools as well, in our case let's say other libraries. Now, we would like to work with a language which provides us extensibility and flexibility in terms of its use. Scala does this. This powerful language lets you mix in newly created traits (you may not have heard about this, but you can compare it to Java's interfaces). There are a number of ways we can make our code more meaningful and of course concise. If used smartly, you can create your own custom constructs with native language features. So this language is as exciting as you are!

This is one of the reasons to learn it. There are other reasons behind why we would choose Scala over any other languages, and there's quite a few. Let's take them one by one. But first let's get confused:

"Scala is a functional language, supports multiple paradigms, and every function in Scala is an object."

Great! Now you know three main characteristics of this language. But it's hard to swallow. It's a functional language, and every function is an object. Really?

The following is an example of a trait defined in Scala, called Function1:

package scala
trait Function1[A, B] {
        def apply(x: A) : B
}

There are more of these, from Function0 to Function22. There's a certain way of using these. We'll be using them many times in this book. We also refer to these as A => B (we call it, A to B). It means this function takes a parameter of type A, does some operation as defined, and returns a value of type B:

val answer = new Functiona1[Int, Int] {
        def apply(x: Int): Int = x * 2
}

This feels a bit too much to start with but getting familiar with these constructs is a good idea. val is a keyword used to declare a value type. It means, once declared and instantiated, you can't change it further. This answer = (x: Int) => x * 2 becomes a function literal that can be passed to another function. We get to this point because we were able to instantiate an object of our Function1 trait (we'll see how this works in Chapter 7, Next Steps in Object-Oriented Scala).

Think of any two lucky numbers, now represent how you can add them. Suppose your numbers were 42 + 61. Here, your numbers 42 and 61 are objects of type Int and + is a method on type Int. This is the way you and Scala are going to treat entities. We'll treat entities as objects and operations performed on them as methods. And this is what makes this language scalable.

We can perform functional operations where inputs are transformed to outputs rather than changing data/state of them. With this in mind, most of our operations (almost all) will not depend on state change; means functions are not going to have side effects. One example could be a function which takes your date of birth and returns your age in terms of the number of years and months:

class YearsAndMonths(years: Int, months: Int)
def age(birthdate: Date): YearsAndMonths = //Some Logic

This is a pure function because it does not manipulate the input. It takes input, transforms, and gives output. Case class is just to help us here define the age in a certain manner. With this, we can introduce more terminology called referentially transparent methods. Our age method can be called referentially transparent. These method calls can be replaced by the result without changing any meaning/semantics of your program.

Pure functions, the concept of immutability, and referential transparency are here only to make this language more powerful. There are more reasons to choose this language as a tool for your next application.