#### Overview of this book

Scala Functional Programming Patterns
Credits
Aknowledgement
www.PacktPub.com
Preface
Free Chapter
Grokking the Functional Way
Singletons, Factories, and Builders
Recursion and Chasing your Own Tail
Lazy Sequences – Being Lazy, Being Good
Taming Multiple Inheritance with Traits
Of Visitors and Chains of Responsibilities
Traversals – Mapping/Filtering/Folding/Reducing
Higher Order Functions
Actors and Message Passing
Index

## Functors

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:

Figure 9.1: Functor translation

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...