Suppose I have functions like this:
val fooXAB: X => A => Try[B] = ...
val fooXBC: X => B => Try[C] = ...
val fooXCD: X => C => Try[D] = ...
I'd like to compose them to make a new function fooXAD: X => A => Try[D]
, which calls fooXAB
, fooXBC
, and fooXCD
sequentially and pass the X
argument to all of them.
Suppose I use scalaz
and have a monad instance for scala.util.Try
. Now I can do it this way:
type AB = Kleisli[Try, A, B]
type BC = Kleilsi[Try, B, C]
type CD = Kleisli[Try, C, D]
type XReader[T] = Reader[X, T]
val fooXAB: XReader[AB] = ...
val fooXBC: XReader[BC] = ...
val fooXCD: XReader[CD] = ...
val fooXAC: XReader[AC] =
for {
ab <- fooXAB
bc <- fooXBC
cd <- fooXCD
} yield (ab andThen bc andThen cd)
Does it make sense? Is it possible to simplify it ?