scalaz - 函数组合 - WriterT

标签 scala scalaz monad-transformers writer-monad

让我们在 \/ 上定义一个 Kleisli:

abstract class MyError
case class NumericalError(msg: String) extends MyError

// Either is a Monad with two type parameters: M[A,B] which represent left and right respectively
// Let's create an ad-hoc type
type EEither[+T] = \/[MyError, T]

和一个用于测试目的的临时函数:

def safeSqrtEither(t: Double): EEither[Double] =
  safeSqrtOpt(t) match {
    case Some(r) => r.right
    case None => NumericalError("Sqrt on double is not define if _ < 0").left
  }
val kSafeSqrtEither = Kleisli.kleisli( (x: Double) => safeSqrtEither(x) )

函数组合工作顺利:

val pipeEither = kSafeSqrtEither >>> kSafeSqrtEither
val r5b = pipeEither2 run 16.0
//which gives r5b: EEither[Double] = \/-(2.0)

我想添加日志:

type LoggedROCFun[I,O] = I => WriterT[EEither,scalaz.NonEmptyList[String],O]
val sqrtWithLog: LoggedROCFun[Double, Double] =
  (t: Double) =>
    WriterT.put(kSafeSqrtEither(t))(s"squared $t".wrapNel)

这似乎具有所需的行为:

val resA = sqrtWithLog(16.0)
// resA: scalaz.WriterT[EEither,scalaz.NonEmptyList[String],Double] = WriterT(\/-((NonEmpty[squared 16.0],4.0)))

时尚。但是,我正在努力组建一个运算符:

  • 合并 WriterT 应用 >>>
  • 中的值
  • 链接(附加)每个日志,跟踪所做的每个步骤

期望的输出:

val combinedFunction = sqrtWithLog >>> sqrtWithLog
val r = combinedFunction run 16.0
// r: WriterT(\/-((NonEmpty[squared 16.0, squared 4.0],2.0)))

我最好的镜头:

def myCompositionOp[I,A,B](f1: LoggedROCFun[I,A])(f2: LoggedROCFun[A,B]): LoggedROCFun[I,B] =
  (x: I) => {
    val e = f1.apply(x)
    val v1: EEither[A] = e.value
    v1 match {
        case Right(v)  => f2(v)
        case Left(err) =>
          val lastLog = e.written
          val v2 = err.left[B]
          WriterT.put(v2)(lastLog)

      }
  }

在上面,我首先将 f1 应用于 x,然后将结果传递给 f2。否则,我将 Left 短路。 这是错误的,因为在这种情况下 正确 我删除了以前的日志记录历史。

最后一个问题

val safeDivWithLog: Kleisli[W, (Double,Double), Double] =
  Kleisli.kleisli[W, (Double, Double), Double]( (t: (Double, Double)) => {
    val (n,d) = t
    WriterT.put(safeDivEither(t))(s"divided $n by $d".wrapNel)
  }
  )
val combinedFunction2 = safeDivWithLog >>> sqrtWithLog
val rAgain = combinedFunction2 run (-10.0,2.0)
// rAgain: W[Double] = WriterT(-\/(NumericalError(Sqrt on double is not define if _ < 0)))

不确定为什么在管道切换到 Left 后日志没有继续执行。是不是因为:

  • type MyMonad e w a = ErrorT e (Writer w) a 同构于 (Either e a, w)
  • type MyMonad e w a = WriterT w (Either e) a 同构于 Either r (a, w)

therefore I have flipped the order

来源:here , scalaz , here , 和 real world haskell on transformers

最佳答案

你非常接近 - 问题只是你把你的 Kleisli 埋了起来,而你想要它在外面。您的 LoggedROCFun 只是一个普通函数,普通函数的 Compose 实例要求第一个函数的输出与第二个函数的输入类型相匹配。如果您将 sqrtWithLog 设为 kleisli 箭头,它就可以正常工作:

import scalaz._, Scalaz._

abstract class MyError
case class NumericalError(msg: String) extends MyError

type EEither[T] = \/[MyError, T]

def safeSqrtEither(t: Double): EEither[Double] =
  if (t >= 0) math.sqrt(t).right else NumericalError(
    "Sqrt on double is not define if _ < 0"
  ).left

type W[A] = WriterT[EEither, NonEmptyList[String], A]

val sqrtWithLog: Kleisli[W, Double, Double] =
  Kleisli.kleisli[W, Double, Double](t =>
    WriterT.put(safeSqrtEither(t))(s"squared $t".wrapNel)
  )

val combinedFunction = sqrtWithLog >>> sqrtWithLog
val r = combinedFunction run 16.0

请注意,为了使其成为一个完整的工作示例,我对您的代码进行了轻微修改。


针对您的评论:如果您希望作者在失败中累积,您需要翻转转换器中 EitherWriter 的顺序:

import scalaz._, Scalaz._

abstract class MyError
case class NumericalError(msg: String) extends MyError

type EEither[T] = \/[MyError, T]

def safeSqrtEither(t: Double): EEither[Double] =
  if (t >= 0) math.sqrt(t).right else NumericalError(
    "Sqrt on double is not define if _ < 0"
  ).left

type W[A] = Writer[List[String], A]
type E[A] = EitherT[W, MyError, A]

val sqrtWithLog: Kleisli[E, Double, Double] =
  Kleisli.kleisli[E, Double, Double](t =>
    EitherT[W, MyError, Double](safeSqrtEither(t).set(List(s"squared $t")))
  )

val constNegative1: Kleisli[E, Double, Double] =
  Kleisli.kleisli[E, Double, Double](_ => -1.0.point[E])

val combinedFunction = sqrtWithLog >>> constNegative1 >>> sqrtWithLog

然后:

scala> combinedFunction.run(16.0).run.written
res9: scalaz.Id.Id[List[String]] = List(squared 16.0, squared -1.0)

请注意,这不适用于编写器中的 NonEmptyList,因为在某些情况下您需要能够返回空日志constNegative1.run(0.0).run.written。我使用了 List,但在实际代码中,您需要一种附加成本较低的类型。

关于scalaz - 函数组合 - WriterT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34357881/

相关文章:

scala - 为什么这个 Scalaz 7 枚举器会泄漏内存?

scala - 使用 Applicative Functor 对选项列表求和

haskell - 用 `catchError`写一串 `(<|>)` -s吗?

Scala包冲突

Scala 2.12.2 发出大量无用的 "Warning: parameter value ... in method ... is never used"警告。如何摆脱它们?

scala - 像 Eithers 这样的错误处理 monad 如何实现引用透明性?

list - 用语义上等效的猫功能替换 scalaz ListT

scala - 我可以在 Scala 中从一组非逗号分隔的标记创建元组吗?

scala - 如何使用 maven-scala 插件解决依赖项的 "error: bad symbolic reference"?

haskell - 从分类的角度看,Haskell 的 monad 转换器是什么?