Scala is a programming language that is growing in popularity.  It is considered both compact and elegant. Plus it supports functional programming, which is basically a means of writing code as mathematical functions, which is often a more natural way of writing code. In other words, it lets you string together doing multiple things all on the same line.

Scala has become popular with data science and big data and is built into Apache Spark.  But it can be used for much more than that, especially as it can instantiate Java objects.  So you could use it to write a full web application, for example.  There are different Scala web frameworks for that also.

Scala code compiles to Java classes.  So you can run it in a JVM. You can also run it in a script or in the interactive interpreter.  You can also run it online in sites like ScalaFiddle.

Here is a short overview.  There is much to learn so we will give just a few samples here and follow up in other blog posts with more examples.

[See Also: NXlog, ElasticSearch, and Kibana]

Objects and Classes

In this example we declare a Class Math with one function add.  Add returns a type Int so we put an = after Int and a colon : after the parameters.

            class Math() {

                        def add(x: Int, y: Int) : Int = {
x + y
}
}

Then we create the object math using the new keyword just as we do with Java.


val math = new Math
println(math.add(2,3))

Run that and the result is:

            5

Declaring Variables

Note that the resulting Int is an object.  In Scala all values are objects, meaning there are no primitives.  So it’s like the class Integer in Java versus the primitive int in Java.  Note also that we declared the object math above with the keyword val.  That means it is immutable, meaning it cannot change.  If we want to declare a variable that we can change we use var.

Variables declaration can either be explicit or implicit:

            val x = 1 // declare Int of value 1

            val x: Int = 1  // explicitly define x as a Int

Note that we use // as the comment delimiter, just like in Java.  We can also use /* comments */.

Import Java Packages

You can use Java objects in Scala programs, like this:

            import java.util.Date

            val now = new Date()

Outputs:

            now: java.util.Date = Sat Oct 15 14:05:50 CLT 2016

Variable Number of Parameters

You can use a variable number of parameters.  So here we have a simple adding machine, with a variable number of integer inputs denoted with an asterisk after Int*.

            class Calc() {

                  var x : Int = 0;

                          def add(ints: Int*)   ={

                                for (int <- ints) {

                                  x += int

                                }

                                      }

                          def answer(): Int = x;

              }

This prints 3:

            var calc = new Calc()

            calc.add(1,2)

            println(calc.answer())

This print 6:

            var calc2 = new Calc()

            calc2.add(1,2,3)

            println(calc2.answer())

Note that you do not need a return statement, although you can use that.  Scala gives back whatever value was calculated last.

[See Also: Introduction to Mesos]

Scala Traits

Traits are like interfaces in Java, where you declare a method as an abstract method in the interface but do not implement it until you reference it in a class.

Below we test whether an integer is even.  We could have another implementation of this trait and define, for example, a String object to be even if it has an even number of letters.

            trait Number {
def isEven (pc : Int)
}

class IntegerClass () extends Number  {
def isEven (pc : Int) = {
println(pc)
if ((pc % 2) == 0) {
println(“true”)
}
}

}

var p = new IntegerClass ()
p.isEven(6)

What does <- Mean in Scala?  For-comprehensions

Scala has some confusing and obscure notations, like <-.  That is used in for-comprehensions.  A for-comprehension is like a Python list comprehension where you can make a list of numbers like {1,2,4,8} the same way a mathematician would, by using function notation.

In math you would write:

            {2x : x ∈ {0,1,2,3,4}}

Which in Python is:

            x = [2**x for x in range (4)]

            print (x)

Outputs:

[1, 2, 4, 8]

In Scala we add a for operation to the list comprehension to get a for-comprehension.  So it is shorthand for doing two things at once.  For example, we create a list {0,1,2,3,4} and then run the for operation on each member of the list and multiply each value by 2:


for (i <- 0 to 4) {
println(i*2)
}

Outputs:

            0
2
4
6
8

 

Everything you need to know about outsourcing technology development
Access a special Introduction Package with everything you want to know about outsourcing your technology development. How should you evaluate a partner? What components of your solution that are suitable to be handed off to a partner? These answers and more below.

Introduction Package

 

Zymr blogger

0 comments

Leave a Reply

© 2019, Zymr, Inc. All Rights Reserved.| LEGAL DISCLAIMER | PRIVACY POLICY | COOKIE POLICY