Scala

Introduction

Scala is a language combining both the features of Object-Oriented Programming and Functional Programming. Since Scala runs on Java Virtual Machine, you can reuse all existing Java Class. In this lab, we will teach you how to write simple Scala programs and explore the object-oriented and functional features of Scala.

Installing the Scala plugin in Eclipse

Choose "Help" -> "Install New Software..." in the menu bar.


  1. Click the "Add..." button.
  2. Type "Scala IDE" in the "Name" field
  3. Copy the link "http://download.scala-ide.org/sdk/helium/e38/scala210/stable/site" into the "Location" field (Note: the previous link is for Eclipse 3.8-4.3; if you use Eclipse 4.4, the link should be "http://download.scala-ide.org/sdk/lithium/e44/scala211/stable/site", see here)


Choose "Scala IDE for Eclipse"


Click the "Next" button.


Accept the licence and then click the "Finish" button.


Import the project used in this lab

First, download the material used in this lab here. Then, unzip the file. It will be used through out this lab.

Then, choose "File" -> "Import..." in the menu bar.


Choose "General" -> "Existing Project into Workspace", and then click the "Next" button.


Click the "Browse" button, and then find the directory of the unzipped lab material.


Click the "Finish" button.


The basic of Scala

Please open the file "HelloWorld.scala" under package "first.example"


Interaction with Java

Please open the file "FrenchDate.scala" under package "interaction.withjava"


Object-Oriented programming

Please open the file "Bicycle.scala" under package "object_oriented.programming"


Functional programming features

Please open the file "FunctionVsNonFunc.scala" under package "functional.programming"

Scala also provides functional programming features. The basic properties of functional programming are

The advantages of functional programming are

Immutable and mutable variable declaration

Immutable is one of the key concept of functional programming. In Scala, a variable can be declared as immutable and mutable, here is an example:

        
object VariableExample{

  def main(args:Array[String]) = {
    //mutable variable
    var mutable_var = 1
    mutable_var = 2

    //immutable variable
    val immutable_var = 1
    immutable_var = 2 //Error !!
  }
}
        
      

Anonymous Function and High Order Function

Please open the file "Sum.scala" under package "functional.programming"

Some advanced features in functional programming:

These two features are useful in our future programming.


Collections in Scala

Please open the file "ScalaCollections.scala" under package "collections.inScala"

Collections (e.g. List, Map, Array, etc.) are very useful classes in Scala. They will ease your job when you are doing analytical tasks.

The map function in Scala works like map() in MR (which turns a collection to a collection).
The reduce function in Scala also works like reduce() in MR (which reduces a collection to a scalar value).

Useful link:

It is difficult to teach everything in single lab. Here are some useful materials for leaning Scala:

Exercise

Now we need to define a function to calculate the square root of a float number:

def sqrt(x: Double): Double = ...

We achieve this by successive approximations using Newton's method. Specifically, to compute sqrt(x):

For example, to compute sqrt(2), the iterations are as follows:

Estimation (y) x/y Mean
1 2/1=2 1.5
1.5 2/1.5=1.333 1.4167
1.4167 2/1.4167=1.4118 1.4142
1.4142 ... ...

Please fill in the blanks in the following code:

object NewtonSqrt {

  //Functions like sqrtIter,improve, and isGoodEnough are ONLY used as the auxiliary functions for sqrt, so we put them inside sqrt.
  def sqrt(target: Double): Double = {

    //Test whether the guess is good enough
    //I.e., test whether Math.abs(guess * guess - target) / target is smaller than a threshold (e.g., 0.001)
    def isGoodEnough(guess: Double): Boolean =
    //snippet 1: PUT YOUR CODE HERE

     //Take the mean of guess and target/guess to improve the guess
    def improve(guess: Double): Double =
    //snippet 2: PUT YOUR CODE HERE

    def sqrtIter(guess: Double): Double =
      if (isGoodEnough(guess)) guess
      else sqrtIter(improve(guess)) //It looks like a if-else in Java, but is used for expressions, not statements

     //The return expression of the function sqrt(target: Double). Here we set the initial guess (i.e., estimate) as 1
    sqrtIter(1.0)
  }

  def main(args: Array[String]) {
    println(sqrt(2.0));
  }
}

Reference Solution:

Snippet 1: if (Math.abs(guess * guess - target) / target < 0.001) true else false

Snippet 2: (guess + target / guess) / 2.0