假设我有这样的功能:
val fooXAB: X => A => Try[B] = ...
val fooXBC: X => B => Try[C] = ...
val fooXCD: X => C => Try[D] = ...
我想将它们组合成一个新函数,它依次fooXAD: X => A => Try[D]
调用fooXAB
、fooXBC
和,并将参数传递给它们。fooXCD
X
假设我使用scalaz
并有一个 monad 实例scala.util.Try
。现在我可以这样做:
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)
是否有意义?是否可以简化它?