gpt4 book ai didi

scala - 如何将 Kleisli 组合与返回验证的函数一起使用?

转载 作者:行者123 更新时间:2023-12-03 22:28:58 26 4
gpt4 key购买 nike

如何组合两个返回验证的函数?以下是我没有成功的尝试:

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

最佳答案

你需要导入 Validation.Monad._ 因为 >=> 需要一个 Bind[M]

scala> import scalaz._, Scalaz._
import scalaz._
import Scalaz._

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> type Va[+A] = Validation[String, A]
defined type alias Va

scala> import Validation.Monad._
import Validation.Monad._

scala> kleisli[Va, Int, Int](f) >=> kleisli[Va, Int, Int](g)
res0: scalaz.Kleisli[Va,Int,Int] = scalaz.Kleislis$$anon$1@4fae3fa6

scala> res0(11)
res1: Va[Int] = Failure(Odd!)

scala> res0(-4)
res2: Va[Int] = Failure(Not positive!)

scala> res0(4)
res3: Va[Int] = Success(9)

关于scala - 如何将 Kleisli 组合与返回验证的函数一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8490198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com