Let's say we have a function, X => Y
. This function is transformed to another function List[X] => List[Y]
. A higher order function that does this transformation is Functor, as shown in the following diagram:
Here, we chose the List
collection for ease of understanding. Instead of List
, any collection will work (Refer to the next section for this):
object Functor extends App { def functor[X, Y](f: X => Y): List[X] => List[Y] = { // 1 def fun : (List[X]) => List[Y] = (arg: List[X]) => arg match { case Nil => Nil case x :: xs => f(x) :: fun(xs) } fun } val p = List("Hi", "there") def doubleEachChar(s: String) = (for (c <- s) // 2 yield c + "" + c).toList.mkString def numberOfLowerCaseChars(s: String) = s.filter(c => c.isLower).length // 3 val f1 = functor(doubleEachChar) // 4 val f2 = functor(numberOfLowerCaseChars) // 5 println(f1(p)) // 6 println(f2(p)) // 7 }
The salient...