Friday, 29 August 2014

Some Scala Interview Questions

This is not for you. It is just a brain dump on Scala interview questions.. I like this site - questions like:

What is tail recursion? A hack to transform a recursive function into a for loop :-). Like the compute method there
What is function currying in Scala? It is a technique of transforming a function that takes multiple arguments into a function that takes a single argument. mult2 is mult1 after currying.
  def mult1(d1: Double, d2: Double) = d1 * d2     //> mult1: (d1: Double, d2: Double)Double
  def mult2(d1: Double) = (d2: Double) => d1 * d2 //> mult2: (d1: Double)Double => Double
  
  mult1(3, 4)                                     //> res0: Double = 12.0
  mult2(4)(3)                                     //> res1: Double = 12.0

Above can be applied to partially applied functions:
  def filter1[T](predicate: T => Boolean)(input: List[T]): List[T] = {
    input match {
      case head::tail => if (predicate(head)) head :: filter1(predicate)(tail) else filter1(predicate)(tail)
      case Nil => Nil
    }
  }                                               //> filter1: [T](predicate: T => Boolean)(input: List[T])List[T]
  val even = (i : Int) => i % 2 == 0              //> even  : Int => Boolean = 
  val odd = (i: Int) => i % 2 != 0                //> odd  : Int => Boolean = 
  
  filter1(even)((0 until 10) toList)              //> res2: List[Int] = List(0, 2, 4, 6, 8)
  filter1(odd)((0 until 10) toList)               //> res3: List[Int] = List(1, 3, 5, 7, 9)
To avoid calling the filter method with the same predicate, another cool variant is this one, using lazy val and _:
  def filter2[T](predicate: T => Boolean)(input: List[T]): List[T] = {
    lazy val rec = filter2(predicate) _
    input match {
      case head::tail => if (predicate(head)) head :: rec(tail) else rec(tail)
      case Nil => Nil
    }
  } 
And lots of other ways with partials and co. Quite neat.
What are implicit parameters? For me they are tricky... without an IDE, ... anyway. Best definition is obviously there
  implicit def myImplicit(i: Int): Boolean = i < 3//> myImplicit: (i: Int)Boolean
  def filter3[T](input: List[T])(implicit predicate: T => Boolean): List[T] = {
    input match {
      case head::tail => if (predicate(head)) head :: filter3(tail) else filter3(tail)
      case Nil => Nil
    }
  }                                               //> filter3: [T](input: List[T])(implicit predicate: T => Boolean)List[T]
  
  filter3[Int]((0 until 10) toList)               //> res5: List[Int] = List(0, 1, 2)

How to create enum? For me, Scala enums are weird...
  object PrimaryColours extends scala.Enumeration {
    type PrimaryColours = Value
    val Red, Green, Blue = Value
    
    def asInts(c: PrimaryColours): (Int, Int, Int) = {
      c match {
        case Red => (255, 0, 0)
        case Green => (0, 255, 0)
        case Blue => (0, 0, 255)
      }
    }
  }
  import PrimaryColours._PrimaryColours.asInts(Red)   //> res6: (Int, Int, Int) = (255,0,0)PrimaryColours.asInts(Red)

Referential transparency? Given a function and a set of inputs, the results will always be the same... no side effect, can be parallelised, cached, etc....

No comments:

Blog Archive