Как я могу использовать композицию Клейсли с функциями, возвращающими проверки?

Как мне составить две функции, возвращающие проверки? Ниже приведены мои попытки, которые не сработали:

scala> def f: Int => Validation[String, Int] = i => if(i % 2 == 0) Success(i * 2) else Failure("Odd!")
f: Int => scalaz.Validation[String,Int]

scala> def g: Int => Validation[String, Int] = i => if(i > 0) Success(i + 1) else Failure("Not positive!")
g: Int => scalaz.Validation[String,Int]

scala> kleisli(f) >=> kleisli(g)
<console>:16: error: no type parameters for method kleisli: (f: A => M[B])scalaz.Kleisli[M,A,B] exist so that it can be applied to arguments (Int => scalaz.Validation[String,Int])
 --- because ---
argument expression's type is not compatible with formal parameter type;
 found   : Int => scalaz.Validation[String,Int]
 required: ?A => ?M[?B]

              kleisli(f) >=> kleisli(g)
              ^

scala> type Va[+A] = Validation[String, A]
defined type alias Va

scala> kleisli[Va, Int, Int](f) >=> kleisli[Va, Int, Int](g)
<console>:17: error: could not find implicit value for parameter b: scalaz.Bind[Va]
              kleisli[Va, Int, Int](f) >=> kleisli[Va, Int, Int](g)
                                       ^

Это работает в Haskell, поэтому я ожидаю, что должен быть способ сделать это и с помощью Scalaz.

λ> let f i = if i `mod` 2 == 0 then Right $ i * 2 else Left "Odd!"
λ> let g i = if i > 0 then Right $ i + 1 else Left "Not positive!"
λ> let h = f >=> g
λ> h 11
Left "Odd!"
λ> h (-4)
Left "Not positive!"
λ> h 4
Right 9
14
задан dave4420 13 December 2011 в 14:25
поделиться