0

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?

caeus
  • 3,084
  • 1
  • 22
  • 36

1 Answers1

3

Finally know how. I can do instead:

val common: F[Common] = ...
val fab = common.flatMap{
  c=>
     commonToA(c).zip(commonToB(c))
}
caeus
  • 3,084
  • 1
  • 22
  • 36