Spark and Scala - Module 2
Spark and Scala - Module 2
ᗍ Lazy Values
ᗍ Functions
ᗍ Procedures
ᗍ Collections
ᗍ Reserved Words
ᗍ Pattern Matching
ᗍ Enumeration
ᗍ Ternary Operators
String result
Integer Result
Double result
Variables are simply names used to refer to some location in memory – a location that holds a value with which we
are working
Values:
Here, myVar is declared using the keyword var. This means that it is a variable that can change value and this is called
mutable variable
When you assign an initial value to a variable, the Scala compiler can figure out the type of the variable based on the
value assigned to it
ᗍ One nice feature built into Scala are "lazy val" values.
ᗍ Lazy value initialization is deferred till it’s accessed for first time
ᗍ For example : If you want to read a file abc.txt, if the file is not existing , you will get FileNotFoundException exception
ᗍ But if you initialize the value as Lazy, you won’t get this error, because it will delay the initialization till it accesses
the file abc.txt
ᗍ Lazy values don’t give error on initialization, whereas no lazy value do give error
Controlled Flow
Start
ᗍ Control Structures controls the flow of execution
doTask1 • if..else
• while
• do-while
doTask2
• foreach
• for
doTask3
Every expression in Scala has a type. First If statement has a type Int
Second statement has a type Any. Type of a mixed expression is supertype of both branches
Syntax:
While(condition)
{
// Block of code ;
}
Note: The ++i, or i++ operators don’t work in Scala, use i+=1 or i=i+1 expressions instead
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time
Syntax:
do
{
//Block of code
} while(condition);
Excluding 5
Including 5
a) Error
b) abcde
c) abcdef
d) None of these
a) Error
b) abcde
c) abcedef
d) None of these
abcde
Syntax:
a) Hi2Welocme
b) Hi200Welcome
c) Error
d) Hi2200Welcome
a) Hi5Welocme
b) Hi200Welcome
c) Error
d) Hi5200Welcome
Error
Such functions are called Procedures. Procedures do not return any value in Scala
Example:
Collections hierarchy
Traversable
Set
IndexedSeq LinearSeq
Mutable collection:
ᗍ This means you can change, add, or remove elements of a collection as a side effect
Immutable collections:
ᗍ You have still operations that simulate additions, removals, or updates, but those operations will in each case return
a new collection and leave the old collection unchanged
Integer Array
String Array
Accessing arrays :
Accessing Arrays:
Adds a list
//Removes an element
it provides the key as the first argument, and then the default value as the second
This class comes with two implementing case classes scala.Nil and scala.:: that implement the abstract members is
Empty, head and tail
Example:
We can use iterator to iterate over a list, but recursion is a preferred practice in Scala
Example:
Here is a second example which matches a value against patterns of different types:
Often we have a variable that can take one of several values. For instance, a WeekDays field of an object could be
either Mon, Tue, Wed, or Thu
In other languages such as C, Java, or Python, it is common to use a small integer to distinguish the possibilities
In Scala, we let the compiler create one object for each possibility, and we use a reference to that object
Example:
Another Example, you can use the Scala ternary operator syntax on the right hand side of the equation, as you
might be used to doing with Java:
a) 3
b) Error
c) 2
d) None of these
a) 3
b) Error
c) 2
d) None of these
Error