In this section we will briefly go through the essential knowledge about Scala. You will first learn how to work with Scala shell, then learn how to use variables, functions with examples. Finally, we give instructions about how to compile and run a standalone program using
You can open a Scala shell by typing
scala. Or, you can use sbt by typing
sbt console. The second approach will help you add your project source code and dependencies into class path, so that your functions or library functions will be available for you to try to in the interactive shell. But in this training, we will stick to Scala shell for simplicity.
Once starting the Scala shell you will see
You can type
:quit to stop and quit the shell, but don't do that now :-) Next you will learn some Scala operations in the shell with the following materials.
In Scala, there are two types of variable, immutable
val and mutable
var. Unlike some functional programming language that requires immutable variables, Scala allows existence of mutable variables but immutable is recommended as it is easier to verify the correctness of your program. Suppose you are still in the Scala interactive shell. Define an immutable variable as
val is a keyword in
scala that makes the variables immutable. If you reassign a value to
myInt, error will be reported.
In interactive shell, it's possible to redefine variable with same name. In Scala source code file, it's not allowed.
Instead, variables declared with
var are mutable. Ideally, we try to use
val instead of
var if possible as a good practice of functional programming.
You may have concern that maybe too many immutable variables will be declared. Actually, with chained function calls, that situation is not the case for well organized code.
An example of mutable variable is
It is always encouraged to specify the type so unless the type is too obvious.
Besides simple built-in variable types like
String, you will also learn about
Tuple in the training:
List[String] is syntax of generics in Scala, which is same as
C#. In the above example,
List[String] means a
(Double, Double) means a two-field tuple type and both the 1st element and the 2nd element should be of type
You can define a function and invoke the function like
x: Int is parameter and its type, and the second
Int is function return type. There's not explicit
return statement, but the result of last expresssion
x*3 will be returned (similar to some other programming languages like
Ruby). In this example, as there is only one expression and return type can be infered by the compiler, you may define the function as
Scala is object-oriented (OO), function calls on a class method are straightforward like most OO languages (e.g. Java, C#)
If the function does not have parameters, you can even call it without parenthesis
You can also define an anonymous function and pass it to variable like a lambda expression in some other languages such as Python:
Anonymous function is very useful as it can be passed as a parameter to a function call
item: String => println(item) is an anonymous function. This function call can be further simplified to
_ represents first parameter of the anonymous function with body
_ can be specified to represent more than one parameter. For example, we can calculate the total payment that a patient made by
In above example,
reduce will aggregate
V and we defined the aggregator as
_ + _ to sum them up. Of course, you can write that more explicitly like
reduce is a construct from functional programming. It can be illustrated with the figure below where a function f is applied to one element at a time and the result together with next element will be parameters of the next function call until the end of the list.
It's important to remember that for
reduce operation, the input is
List[V] and the output is
V. Interested reader can learn more from wiki. In contrast to
reduce, you can of course write code using
for loop, which is verbose and very rare in Scala,
Declaration of a class in Scala is as simple as
Here we see the succinct syntax of Scala again.
class Patient(val name: String, val id: Int) not only defines constructor of
Patient but also defines two member variables (
A special kind of class that we will use a lot is
Case Class can be declared as
and see below Pattern Matching for use case.
You may know the
switch..case in other languages. Scala provides a more flexible and powerful technique,
Pattern Matching. Below example shows one can match by-value and by-type in one match.
It's very convenient to use case class in pattern matching
Here we not only matched
Patient type, but also matched patient name and extracted one member field from the
Patient class instance.
Working with large real-world applications, you usually need to compile and package your source code with some tools. Here we show how to compile and run a simple program with sbt. Run the sample code in 'hello-bigdata' folder
The source code file
hello.scala is compiled and invoked.
This is a very brief overview of important features from the Scala language. We highly recommend readers to checkout references below to get a better and more complete understanding of Scala programming language.