Introduction to Scala for enterprise Java developers

Here we begin a series of articles to look at Scala.

Scala is a programming language that rides atop the Java virtual machine. In compiles to Java bytecode and can be packaged in Jar files. Scala and Java are so closely related that you can use Java packages in Scala programs. But Scala is not nearly as wordy as Java, which is Java’s chief complaint. But is offers more than just brevity.

Some important products were built using Scala, including Apache Spark and Kafka. The latter was written by LinkedIn. Spark has a Scala command line interface. Twitter also use Scala, says the Scala language organization. Netflix uses it too. Netflix has always been a company that gives away a lot of their code. For example, here is the Scala Atlas API on Github which Netflix says is a “backend for managing dimensional time series data.”

Some have said that Scala is difficult to learn because it is a functional programming language. Briefly that means it can look more like mathematical statements than the if-then-else style of programming that most people are used too. There is even a word for making Scala, or any, code smaller and tighter thus functional programming-like: sugar syntax.

But you are not forced to write Scala code that is so terse that other people cannot read in.

Running Scala Code

Scala code can be run two ways. You can create a someName.sc file and then run the command scala someName.sc. That is because Scala is an interpreted language interface as well as a compiled one. So it also has a command line shell into which you can type commands directly and have them evaluated on the spot, aka a REPL (read, evaluate, print, loop), like Python. But for a program that uses external packages, you would want to use sbt, which is the Scala build tool, to build a Jar file. So sbt is like Maven or Ant.

Basic Examples

Scala has maps, tuples, arrays, sets, lists, and something called options.

Below we declare a List with three elements. Notice that there is no need put end the statement with a semicolon (;), but you can. Also note the use of the word var to declare a variable. If we wanted to declare an immutable variable (i.e., one that cannot change) we would have written val.

In the interpreter, the first line we typed below is the code. The second line is the response, or Scala acknowledging what we just entered.

var n = List(1,2,3)
n: List[Int] = List(1, 2, 3)

Note above the Scala says that n is a List of Int. You can be explicit when you define variables such as:

variable: type

Collections

A list is an iterable object, or a collection. That means we can run a map operation over it, meaning perform some operation over each member. Scala facilitates this by letting you implicitly declare variables inline and use an inline function all in one step.

For example, below the x => syntax means create a variable x and pass it into a function. Then we multiply x by x in the function. We picked x here. We would have picked any letter.

var m = n.map( x => x*x)
m: List[Int] = List(1, 4, 9)

We can also use the items in the List to make another object, say a tuple.

First we declare a list:

var words = List("apple", "apple", "orange")

Then make tuples (x,1):

var grp = words.map(x => (x,1))
grp: List[(String, Int)] = List((apple,1), (apple,1), (orange,1))

You add 1 to a tuple when you want to write the HelloWord-type WordCount function.

Introduction to Scala Maps

A map is a (key,value) pair.

var grp:Map[String,Int] = Map(("a",1),("b",2),("c",3))
grp: Map[String,Int] = Map(a -> 1, b -> 2, c -> 3)

Here, for example, are the keys.

grp.keys
res46: Iterable[String] = Set(a, b, c)

Use Java Classes

We can use Java classes with Scala:

import java.lang.Math
Math.PI

res20: Double = 3.141592653589793

Functions

Scala functions have the form:

def functionName (parameter: Type) : returnType = { code }

as in:

def square (x : Double) : Double = { return x*x }

You can leave off the word return. Then the last value calculated is what is returned.

Classes

Classes are just like Java classes, with member functions and variables. Scala uses Traits, which are like Java interfaces and abstract classes.

class Cat (var breed: String) {

def whatBreed(): String = { return breed }
}


var alleyCat = new Cat("alley cat")
alleyCat: Cat = Cat@637bf67c

alleyCat.whatBreed()

res15: String = alley cat

Notice that functions have value, so if you do not assign one explicitly Scala will do that by itself. Here it as created a new value res15. When we ask Scala is res15 gives than and assigns yet another new value res16.

res15
res16: String = alley cat

Because functions have value, we can pass them to other functions. We will illustrate that in another post.

Introduction to Scala Traits

Traits are like Java interfaces or abstract classes. A class can extend one more multiple traits.

trait Coffee {
def isPricey(price: Float): Boolean = { (price > 100) }
}

class GoldenBlend extends Coffee {
}

Now create an instance of Coffee. It inherits the isPricey method:

var coffee = new GoldenBlend()
coffee: GoldenBlend = GoldenBlend@517c6a67

scala> coffee.isPricey(200)
res44: Boolean = true

 

And that’s a quick introduction to Scala for enterprise Java developers. There is a lot more Scala to learn, with functional programming topping the list. It’s a topic we will broach in the follow up tutorial.