Scala Quick Notes :: Part - 6
Bhaskar S | 02/08/2015 |
Overview
In this part of the series, we will look at the Functional capabilities of Scala such as Anonymous functions, First-Class functions, Higher-Order functions, and Closures.
Hands-on With Scala - VI
The following is the Scala program named Sample19.scala:
Executing the program Sample19.scala results in the output:
Value from fn1 = Welcome to Functional Scala Value from fn2 = 55 List from fn3 = List(6, 7, 8, 9, 10) List from fn4 = List(5, 10, 15, 20, 25)
The following section explains some of the aspects of the Scala program Sample19.scala:
A Scala Anonymous function (also referred to as a Function Literal) is an un-named function that can be defined in a compact form using the syntax: (Function Parameters) => { Function Body }
Ex: (n: Int) => List.range(1, n+1).sum
A First-Class function in Scala is a function that can be stored in a variable (just like another other Scala object), passed as a parameter to a function or returned as a value from a function call.
The type of an Anonymous or First-Class function is specified using the syntax: (Input Parameter Types) => Output Type
Ex: val fn4: (List[Int], Int) => List[Int] = multiplier
Notice the use of the yield keyword in the for loop. For each iteration of the for loop, yield produces a value that is internally saved (in the order) but not immediately returned. When the for loop terminates, all the values produced by yield are returned as a collection. The type of the collection returned is of the same type as the one the for loop iterated over. In our example, it is a List. This is often times referred to as For Comprehension
The following is the Scala program named Sample20.scala:
Executing the program Sample20.scala results in the output:
List l1 = List(4, 5, 6, 7, 8) List l2 = List(3, 6, 9, 12, 15) List l3 = List(0, 0, 1, 1, 1) [02/08/2015 - 10:58:40]: Line precede by date and time [02/08/2015]: Line precede by date [10:58:40]: Line precede by time [02/08/2015 - 10:58:40]: Line precede by date and time
The following section explains some of the aspects of the Scala program Sample20.scala:
A Higher-Order function is a function that takes one or more function types as input parameters
Ex: def listTransform(a: List[Int], b: Int, fn: (Int, Int) => Int): List[Int] = { ... }
A Higher-Order function can also return a function type
Ex: def logger2(a: String = "DT") = { ... (b: String) => printf("%s: %s\n", fmt.format(now), b) }
The following is the Scala program named Sample21.scala:
Executing the program Sample21.scala results in the output:
ALERT: High Temperature 101F !!! ALERT: High Temperature 104F !!! Is 7 a factor of 39 ?: false Is 7 a factor of 56 ?: true Is 13 a factor of 139 ?: false Is 13 a factor of 221 ?: true Alice your deductible is 50 Bob your deductible is 20
The following section explains some of the aspects of the Scala program Sample21.scala:
A Closure is a function that not only depends on its input parameters and local variables, but also variables defined outside its own scope. The name Closure comes from the act of “closing” or “capturing” variables defined outside the function scope
Ex: def tempartureAlert(a: Int) = if (a > tempThreshold) printf(s"ALERT: High Temperature ${a}F !!!\n")
When a Scala Closure captures a variable defined using the keyword var, it will see any changes made to the variable outside the Closure. Similarly, any changes made to the variable by the Closure is also visible to the outside
Ex: tempThreshold = 103; tempartureAlert(101); tempartureAlert(104)
When a Scala Closure captures a local variable defined inside a function, it is the local variable instance (or value) that is active at the time of the Closure creation that is captured
Ex: val isFact7 = isFactorsOf(7)
Ex: def issueHealthCard(a: String) = { ... }
References