September 2018
Intermediate to advanced
398 pages
9h 43m
English
Whenever you have a function A => B, you can lift it to make a function F[A] => F[B], as long as you have a Functor[F] instance in scope, as follows:
def square(x: Double): Double = x * x def squareVector: Vector[Double] => Vector[Double] = Functor[Vector].lift(square) squareVector(Vector(1, 2, 3)) // res0: Vector[Double] = Vector(1.0, 4.0, 9.0) def squareOption: Option[Double] => Option[Double] = Functor[Option].lift(square) squareOption(Some(3)) // res1: Option[Double] = Some(9.0)
Another handy function is fproduct, which tuples value with the result of applying a function:
Vector("Functors", "are", "great").fproduct(_.length).toMap //res2: Map[String,Int] = Map(Functors -> 8, are -> 3, great -> 5)
We created Vector[(String, ...
Read now
Unlock full access