September 2018
Intermediate to advanced
398 pages
9h 43m
English
This is all good, but in practice, I rarely put functions inside of contexts. I find that the function map2 in Apply is more useful. It is defined in Apply by using product and map. The product defines itself, using ap and map:
trait Apply[F[_]] extends Functor[F] … { (...) def map2[A, B, Z](fa: F[A], fb: F[B])(f: (A, B) => Z): F[Z] = map(product(fa, fb))(f.tupled) override def product[A, B](fa: F[A], fb: F[B]): F[(A, B)] = ap(map(fa)(a => (b: B) => (a, b)))(fb)
The map2 object allows for applying a function to two values inside of an F context. This can be used to combine two values inside F, as follows:
def parseIntO(s: String): Option[Int] = Either.catchNonFatal(s.toInt).toOption parseIntO("6").map2(parseIntO("2"))(_ ...
Read now
Unlock full access