a type of mapping between categories arising in category theory.
Functors can be thought of as homomorphisms between categories
val opt: Option[Int] = Some(3)
val intToString: Int => String = (i: Int) => i.toString
// apply map from type A (Int) to type B (String)
val res: Option[String] = opt.map(i => intToString(i))
> Some("3")
an endofunctor (a functor mapping a category to itself), together with two natural transformations.
def apiCall(url: String): Task[WebResponse] = { ... }
def saveToDatabase(resp: WebResponse): Task[DatabaseResponse] = { ... }
val dbResp: Task[DatabaseResponse] =
apiCall("http://google.com").flatMap { webResponse =>
saveToDatabase(webResponse)
}
// Identity law
Some(3).map(identity) == Some(3)
// Composition
val plusOne: Int => Int = (x: Int) => x + 1
val timesThree: Int => Int = (y: Int) => y * 3
Some(3).map(plusOne).map(timesThree) == Some(3).map(plusOne compose timesThree)