Scala Quick Notes :: Part - 7


Bhaskar S 02/15/2015


Overview

In this part of the series, we will continue to look at the Functional capabilities of Scala such as Partially Applied functions, Curried functions, Partial functions, and Recursive functions.

Hands-on With Scala - VII

The following is the Scala program named Sample22.scala:

Sample22.scala
/*
 *
 * Name  : Sample22
 * 
 * Author: Bhaskar S
 *  
 * Date  : 02/15/2015
 *  
 */

package com.polarsparc.scala

object Sample22 {
  def main(args: Array[String]) = {
    def modulus(a: Int, b: Int): Int = a % b
    
    printf("modulus(5, 2) = %d\n", modulus(5, 2))
    
    val mod2 = modulus(_: Int, 2)
    
    printf("mod2 = %s\n", mod2)
    printf("mod2(5) = %d\n", mod2(5))
    
    val modulus2 = new Function1[Int, Int] {
      override def apply(a: Int) = a % 2
    }
    
    printf("modulus2 = %s\n", modulus2)
    printf("modulus2(5) = %d\n", modulus2(5))
    
    def compute(a: Double, b: Double, c: Int): Double = (a + b)/c
    
    printf("compute(2, 3, 4) = %.02f\n", compute(2, 3, 4))
    
    val average = compute(_: Int, _: Int, 2)
    
    printf("average = %s\n", average)
    printf("average(2, 3) = %.02f\n", average(2, 3))
    
    val average2 = new Function2[Double, Double, Double] {
      override def apply(a: Double, b: Double) = (a + b)/2
    }
    
    printf("average2 = %s\n", average2)
    printf("average2(2, 3) = %.02f\n", average2(2, 3))
  }
}

Executing the program Sample22.scala results in the output:

Output (Sample22.scala)

modulus(5, 2) = 1
mod2 = <function1>
mod2(5) = 1
modulus2 = <function1>
modulus2(5) = 1
compute(2, 3, 4) = 1.25
average = <function2>
average(2, 3) = 2.50
average2 = <function2>
average2(2, 3) = 2.50

The following section explains some of the aspects of the Scala program Sample22.scala:

The following is the Scala program named Sample23.scala:

Sample23.scala
/*
 *
 * Name  : Sample23
 * 
 * Author: Bhaskar S
 *  
 * Date  : 02/15/2015
 *  
 */

package com.polarsparc.scala

object Sample23 {
  def main(args: Array[String]) = {
    def logMsg(a: String)(b: String) = a + " :: " + b
    
    printf("logmsg('INFO')('Information 1') = %s\n", logMsg("INFO")("Information 1"))
    
    val infolog = logMsg("INFO") _
    
    printf("infolog('Information 2') = %s\n", infolog("Information 2"))
    
    val warnlog = logMsg("*WARN*") _
    
    printf("warnlog('This is a Warning') = %s\n", warnlog("This is a warning"))
    
    def sizedLog(a: Int)(b: String)(c: String) = {
      if (a > 0)
        (b + " :: " + c).take(a)
      else
        (b + " :: " + c)
    }
    
    val logSize20 = sizedLog(20) _
    
    val infoLogSize20 = logSize20("INFO")
    
    printf("infoLogSize20('This is long information') = %s\n", infoLogSize20("This is long information"))
  }
}

Executing the program Sample23.scala results in the output:

Output (Sample23.scala)

logmsg('INFO')('Information 1') = INFO :: Information 1
infolog('Information 2') = INFO :: Information 2
warnlog('This is a Warning') = *WARN* :: This is a warning
infoLogSize20('This is long information') = INFO :: This is long

The following section explains some of the aspects of the Scala program Sample23.scala:

The following is the Scala program named Sample24.scala:

Sample24.scala
/*
 *
 * Name  : Sample24
 * 
 * Author: Bhaskar S
 *  
 * Date  : 02/15/2015
 *  
 */

package com.polarsparc.scala

object Sample24 {
  def main(args: Array[String]) = {
    val dayOfWeek = new PartialFunction[Int, String] {
      override def apply(a: Int) =  a match {
        case 1 => "Sun"
        case 2 => "Mon"
        case 3 => "Tue"
        case 4 => "Wed"
        case 5 => "Thu"
        case 6 => "Fri"
        case 7 => "Sat"
        case _ => "???"
      }
      
      override def isDefinedAt(a: Int) = a > 0 && a < 8
    }
    
    printf("dayOfWeek(1) = %s\n", dayOfWeek(1))
    printf("dayOfWeek(5) = %s\n", dayOfWeek(5))
    
    if (dayOfWeek.isDefinedAt(9))
      printf("dayOfWeek(9) = %s\n", dayOfWeek(9))
    
    val dayOfWeek2: PartialFunction[Int, String] = {
      case a: Int if a > 0 && a < 8 => a match {
        case 1 => "Sun"
        case 2 => "Mon"
        case 3 => "Tue"
        case 4 => "Wed"
        case 5 => "Thu"
        case 6 => "Fri"
        case 7 => "Sat"
        case _ => "???"
      }
    }
    
    printf("dayOfWeek2(1) = %s\n", dayOfWeek2(1))
    printf("dayOfWeek2(5) = %s\n", dayOfWeek2(5))
    
    if (dayOfWeek2.isDefinedAt(9))
      printf("dayOfWeek2(9) = %s\n", dayOfWeek2(9))
    	
    val dayOfWeek3 = dayOfWeek.lift
    
    printf("dayOfWeek3(2) = %s\n", dayOfWeek3(2))
    printf("dayOfWeek3(0) = %s\n", dayOfWeek3(0))
  }
}

Executing the program Sample24.scala results in the output:

Output (Sample24.scala)

dayOfWeek(1) = Sun
dayOfWeek(5) = Thu
dayOfWeek2(1) = Sun
dayOfWeek2(5) = Thu
dayOfWeek3(2) = Some(Mon)
dayOfWeek3(0) = None

The following section explains some of the aspects of the Scala program Sample24.scala:

The following is the Scala program named Sample25.scala:

Sample25.scala
/*
 *
 * Name  : Sample25
 * 
 * Author: Bhaskar S
 *  
 * Date  : 02/14/2015
 *  
 */

package com.polarsparc.scala

object Sample25 {
  def main(args: Array[String]) = {
    def factorial(n: Int): BigInt = {
       if (n < 2) 1 else n*factorial(n-1)
    }
    
    printf("factorial(5) = %d\n", factorial(5))
    printf("factorial(10) = %d\n", factorial(10))
    printf("factorial(100) = %d\n", factorial(100))
    printf("factorial(1000) = %d\n", factorial(1000))
    
    // printf("factorial(10000) = %d\n", factorial(10000)) -- Will generate a java.lang.StackOverflowError
    
    import scala.annotation.tailrec
    def factorial2(n: Int): BigInt = {
      @tailrec
      def factorial2TailRec(n: Int, accumulator: BigInt): BigInt = {
        if (n < 2) accumulator else factorial2TailRec(n-1, n*accumulator)
      }
      factorial2TailRec(n, 1)
    }
    
    printf("factorial2(1000) = %d\n", factorial2(1000))
    printf("factorial2(10000) = %d\n", factorial2(10000))
  }
}

Executing the program Sample25.scala results in the output:

Output (Sample25.scala)

factorial(5) = 120
factorial(10) = 3628800
factorial(100) = 9332621544394415268169923885626670049071596826438162146859296389521...
factorial(1000) = 402387260077093773543702433923003985719374864210714632543799910429...
factorial2(1000) = 40238726007709377354370243392300398571937486421071463254379991042...
factorial2(10000) = 2846259680917054518906413212119868890148051401702799230794179994...

The following section explains some of the aspects of the Scala program Sample25.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

Scala Quick Notes :: Part - 6