This is a working reference to the core Scala language commands every Spark developer runs into early on, expressions, values, functions, classes, loops, and collections, each with a short explanation and example.
An expression is simply a computable statement. Typed into the Scala shell, it evaluates immediately and returns a result.
scala> 12 * 10
res0: Int = 120

You can print the result of an expression directly using println.

scala> println(“Hello, ” + “Prwatech”)

The val keyword names the result of an expression. Referencing a value later does not recompute it, it stays fixed.
Here such as ‘x’ is value “Referencing a value doesn’t re-compute it”

Variables behave like values, except you can reassign them. Define one with the var keyword.
var x = 6 + 4
x = 10

Surrounding a group of expressions with curly braces combines them into a block. The result of the last expression inside the block becomes the result of the block as a whole.


Functions are expressions that take parameters. You can define an anonymous function, one with no name, using arrow syntax, where the parameter list sits to the left of the arrow and the expression using it sits to the right.
On the left of =,> is a list of parameters. On the right is an expression involving the parameters.
You can also name functions.

Functions can also be given a name.

A function may take multiple parameters.

Or it can take no parameters at all.

Methods look and behave much like functions, with a few key differences. A method is defined with the def keyword, and its return type is declared after the parameter list, following a colon.

A method can also take multiple parameter lists, one set of parentheses after another.

You can define classes with the class keyword followed by its name and constructor parameters.

Objects are single instances of their own definitions. You can think of them as singletons of their own classes.
You can define objects with the object keyword.

Scala classes do not have a static keyword, so a singleton object fills that role instead. Its constructor runs the first time the object is used, it carries all the features of a class, and its constructor cannot take parameters. A singleton object can be imported from anywhere in the program.
Singleton objects are commonly used in a few situations:

var a = 8
val s = if (a > 1 && a < 9) 1 else 0
var a = 9
val s = if (a > 1 && a < 9) 1 else 0

var args = “PRWATECH”
var i = 0
while (i < args.length) {
println(args(i))
i += 1
}

var args = “SCALA”
args.foreach(println)

for (i <- 1 to 5) print(i)
for (i <- 11 to 25) print(i)

for (i <- 1 to 5; j = i) println(20 * i + j)

Define the function first, then call it with a value.
def area(radius: Int): Double = { 7.20 * radius * radius }

def concatstr(arg1: String, arg2: String = “Institute”, arg3: String = “Prwatech”) = println(arg1 + arg2 + arg3)
concatstr(“BigData”)
concatstr(“BigData”, arg3 = “spark”)
concatstr(“BigData”)
Parameters can be given default values, so a caller only needs to supply the ones they want to override, such as arg3 in the second call above.
Scala has a special kind of function that returns no value. If a function is written without a preceding equals sign, its return type becomes Unit, and a function like this is called a procedure. Procedures never return a value in Scala.
def rect_area(length: Float, breadth: Float) { val area = length * breadth; println(area) }

Scala has a rich collections library. The most commonly used are Array, ArrayBuffer, Map, Tuple, and List.
An array is a collection of elements or records of the same type.
var i = Array(“Hello”, “Prwatech”)
print(i(0))
print(i(1))

An ArrayBuffer is like an array, but it can grow or shrink in size, unlike a fixed size Array. You bring it in with an import statement before using it.
import scala.collection.mutable.ArrayBuffer
var buffer = ArrayBuffer(“Hello”, “Prwatech”)
buffer += “Spark”
print(buffer)
A Map is a collection of key and value pairs, similar to a Java HashMap or a Python dictionary. You look up a value by its key rather than by position.
var colors = Map(“red” -> “#FF0000”, “yellow” -> “#FFFF00”)
print(colors(“yellow”))
A Tuple holds a fixed group of values that can be of different types in the same tuple, unlike an Array, and it is immutable. You access each element with an underscore followed by its position, starting from 1.
var t = (15, “Prwatech”, true)
print(t._1)
print(t._2)
A List is an immutable, ordered collection of elements of the same type, similar to an Array, but a List cannot be resized once created.
var l = List(“Hello”, “Prwatech”, “Spark”)
print(l(0))
That covers the core Scala commands used in Spark development, from expressions and functions through classes, loops, and collections. To go further, explore Prwatech’s Apache Spark training program, which includes placement assistance.