Search code examples
scalafunctional-programmingmonadsscalazscala-cats

Fork Join with generic monad scala?


Is it possible to express in a generic way, using cats or scalaz this?

val common: F[Common] = ...
val a: F[A] = common.flatMap(commonToA)
val b: F[B] = common.flatMap(commonToB)
val result: F[(A,B)] =  a someFunctionToProduct b

And ensure that the common effect is effectively executed only once?


Solution

  • Finally know how. I can do instead:

    val common: F[Common] = ...
    val fab = common.flatMap{
      c=>
         commonToA(c).zip(commonToB(c))
    }