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:

Sample19.scala
/*
 *
 * Name  : Sample19
 * 
 * Author: Bhaskar S
 *  
 * Date  : 02/07/2015
 *  
 */

package com.polarsparc.scala

object Sample19 {
  def main(args: Array[String]) = {
    var fn1 = () => "Welcome to Functional Scala"
    
    printf("Value from fn1 = %s\n", fn1())
    
    var fn2 = (n: Int) => List.range(1, n+1).sum
    
    printf("Value from fn2 = %d\n", fn2(10))
    
    var fn3 = (a: List[Int], b: Int) => {
      for (i <- a) yield i+b
    }
    
    printf("List from fn3 = %s\n", fn3(List[Int](1, 2, 3, 4, 5), 5))
    
    def multiplier(a: List[Int], b: Int): List[Int] = {
      for (i <- a) yield i*b
    }
    
    val fn4: (List[Int], Int) => List[Int] = multiplier
    
    printf("List from fn4 = %s\n", fn4(List[Int](1, 2, 3, 4, 5), 5))
  }
}

Executing the program Sample19.scala results in the output:

Output (Sample19.scala)

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:

The following is the Scala program named Sample20.scala:

Sample20.scala
/*
 *
 * Name  : Sample20
 * 
 * Author: Bhaskar S
 *  
 * Date  : 02/07/2015
 *  
 */

package com.polarsparc.scala

import java.util.Calendar
import java.text.SimpleDateFormat

object Sample20 {
  def main(args: Array[String]) = {
    def opAdd(a: Int, b: Int): Int = a + b
    
    def opMul(a: Int, b: Int): Int = a * b
    
    def listTransform(a: List[Int], b: Int, fn: (Int, Int) => Int): List[Int] = {
      for (i <- a) yield fn(i, b)
    }
    
    val l1 = listTransform(List[Int](1, 2, 3, 4, 5), 3, opAdd)
    
    printf("List l1 = %s\n", l1)
    
    val l2 = listTransform(List[Int](1, 2, 3, 4, 5), 3, opMul)
    
    printf("List l2 = %s\n", l2)
    
    val l3 = listTransform(List[Int](1, 2, 3, 4, 5), 3, (a: Int, b: Int) => a/b)
    
    printf("List l3 = %s\n", l3)
    
    val fmt1 = new SimpleDateFormat("[MM/dd/yyyy - HH:mm:ss]")
    
    def logger1() = (a: String) => {
      val now = Calendar.getInstance().getTime()
      
      printf("%s: %s\n", fmt1.format(now), a)
    }
    
    val lg1 = logger1()
    
    lg1("Line precede by date and time")
    
    val fmt2 = new SimpleDateFormat("[MM/dd/yyyy]")
    val fmt3 = new SimpleDateFormat("[HH:mm:ss]")
    
    def logger2(a: String = "DT") = {
      val now = Calendar.getInstance().getTime()
      
      val fmt = a match {
        case "D" => fmt2
        case "T" => fmt3
        case _ => fmt1
      }
      
      (b: String) => printf("%s: %s\n", fmt.format(now), b)
    }
    
    val lg2 = logger2("D")
    
    lg2("Line precede by date")
    
    val lg3 = logger2("T")
    
    lg3("Line precede by time")
    
    val lg4 = logger2()
    
    lg4("Line precede by date and time")
  }
}

Executing the program Sample20.scala results in the output:

Output (Sample20.scala)

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:

The following is the Scala program named Sample21.scala:

Sample21.scala
/*
 *
 * Name  : Sample21
 * 
 * Author: Bhaskar S
 *  
 * Date  : 02/07/2015
 *  
 */

package com.polarsparc.scala

object Sample21 {
  def main(args: Array[String]) = {
    var tempThreshold = 100
    
    def tempartureAlert(a: Int) = if (a > tempThreshold) printf(s"ALERT: High Temperature ${a}F !!!\n")
    
    tempartureAlert(100)
    tempartureAlert(101)
    
    tempThreshold = 103
    
    tempartureAlert(101)
    tempartureAlert(104)
    
    def isFactorsOf(b: Int) = (a: Int) => if (a % b == 0) true else false
    
    val isFact7 = isFactorsOf(7)
    val isFact13 = isFactorsOf(13)
    
    printf("Is 7 a factor of 39 ?: %s\n", isFact7(39))
    printf("Is 7 a factor of 56 ?: %s\n", isFact7(56))
    printf("Is 13 a factor of 139 ?: %s\n", isFact13(139))
    printf("Is 13 a factor of 221 ?: %s\n", isFact13(221))
    
    def issueHealthCard(a: String) = {
      val deduct1 = 50
      val deduct2 = 20
      
      val d = a match {
        case "hmo" => deduct1
        case _ => deduct2
      }
      
      (b: String) => printf(s"${b} your deductible is ${d}\n")
    }
    
    val hmo = issueHealthCard("hmo")
    val ppo = issueHealthCard("ppo")
    
    hmo("Alice")
    ppo("Bob")
  }
}

Executing the program Sample21.scala results in the output:

Output (Sample21.scala)

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:

References

Scala Quick Notes :: Part - 1

Scala Quick Notes :: Part - 2

Scala Quick Notes :: Part - 3

Scala Quick Notes :: Part - 4

Scala Quick Notes :: Part - 5