Spark and Scala_Module 4
Spark and Scala_Module 4
ᗍ Traits as Mixins
ᗍ Functional Programming
ᗍ Paradigms of Functional Programming
ᗍ Higher Order Functions
ᗍ Currying
ᗍ Closures
ᗍ Anonymous Blocks,
ᗍ Implicit Function Parameters
ᗍ Call by Name
ᗍ Call by Value
Example:
Class Person {
def id: String ...
.....
}
Class Employee{
def id: String ...
...
}
Example:
ᗍ Unlike Java, Scala allows traits to be partially implemented; i.e. it is possible to define default implementations for
some methods
ᗍ Methods need not be declared as abstract
ᗍ An unimplemented method is automatically assumed as abstract method
ᗍ Once a trait is defined, it can be mixed in to a class using either the keyword extends or the keyword with
ᗍ We can use methods inherited from a trait just as any method inherited from a super class
ᗍ Once a trait is defined we get a new type, similarly to defining a new class
The concrete trait method provides log method with its implementation
ᗍ Now when Top method is executed, the else part of the Logger1 is invoked and displays the message
ᗍ For the class-private field, private getter and setter are generated
ᗍ Thus we get the flexibility of attaching use case specific loggers!
a. True
b. False
a. True
b. False
False
ᗍ Apart from being a pure object oriented language, Scala is also a functional programming language
ᗍ Functional programming is driven by mainly two ideas:
• First main idea is that functions are first class values. They are treated just like any other type, say String,
Integer etc. So functions can be used as arguments, could be defined in other functions
• The second main idea of functional programming is that the operations of a program should map input
values to output values rather than change data in place. This results in the immutable data structures
Take the sum of the squares of all the integers between a and b:
b
Σf(n)
n=a
Can we factor out the common pattern?
Where
foreach
(1 to 10).map("*" * _).foreach(println _)
reduceLeft
(1 to 5).reduceLeft(_ * _)
Split, sortWith
Instead of
def str = ”skillspeed”; println(str)
println(”skillspeed”)
ᗍ Because strings exist as literals. Analogously we would like function literals, which let us write a function without
giving it a name
ᗍ These are called anonymous functions
ᗍ Here, (x: Int) is the parameter of the function, and x * x * x is it’s body
ᗍ The type of the parameter can be omitted if it can be inferred by the compiler from the context
ᗍ If there are several parameters, they are separated by commas:
This simplification of syntax is also more commonly and formally known as Syntactic Sugar
a. True
b. False
a. True
b. False
False
a. True
b. False
a. True
b. False
True
ᗍ If a function has only one parameter, then the “()” can be omitted, hence now it can be expressed as:
value(x => 4 * x)
ᗍ If the parameter is used only ONCE on right side of =>, then it can be replaced with underscore “_” . So now
value(4 * _)
ᗍ When defining a function, at runtime we get an object. Each function call is actually an object. When we define a
function that uses variables from its outer scope the object that we get is a closure
ᗍ Example:
ᗍ This is called Closure. So, a Closure comprises of code along with the definition of non-local variables used by the
code
ᗍ Currying is the technique of transforming a function that takes multiple arguments into a function that takes a
single argument
Example:
//before currying
add(1, 2) // 3
add(7, 3) // 10
//After Currying
add(1)(2) // 3
add(7)(3) // 10
ᗍ A method with implicit parameters can be applied to arguments just like a normal method
ᗍ In this case the implicit label has no effect
ᗍ However, if such a method misses arguments for its implicit parameters, such arguments will be automatically
provided
Call by Value:
ᗍ Typically, parameters to functions are by-value parameters; that is, the value of the parameter is determined before
it is passed to the function
ᗍ In most circumstances, this is the behaviour we want and expect
a. True
b. False
a. True
b. False
False
Closures:
Closures:
Can access out of scope variable and Essentially are anonymous functions