- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
让我们在 \/
上定义一个 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
后日志没有继续执行。是不是因为:
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
请注意,为了使其成为一个完整的工作示例,我对您的代码进行了轻微修改。
针对您的评论:如果您希望作者在失败中累积,您需要翻转转换器中 Either
和 Writer
的顺序:
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/
我正在尝试学习 scalaz7 镜头。有没有更好的方法来链接集合操作? case class Outer(left: Inner, right: Inner) case class Inner(top
我需要使用 scalaz 中的 EqualsOps (===),但是导入 scalaz.Scalaz._ 给我带来了与 anorm 的 get 方法的命名冲突。 这是编译错误: reference t
这个问题与this one有关,我试图了解如何在 Scala 中使用 reader monad。 在答案中,作者使用以下代码获取 ReaderInt[String] 的实例: import scala
在 Nick Partridge's presentation on deriving scalaz ,基于旧版本的 scalaz,他使用一个函数引入了验证: def even(x: Int): Va
难以理解 scalaz 中 === 的行为 1. scala> 1.some === Some(1) res33: Boolean = true 2. scala> Some(1) === 1.
我正在尝试根据 question 调整我的代码.一切都很好,除了我的方法返回 scalaz.\/ 而不是 scala.util.Either。所以现在我必须写一个像这样的猴子代码: def myFun
简介 我使用Scalaz 7在许多项目中进行迭代,主要用于处理大型文件。我想开始切换到 Scalaz streams ,它们旨在替换 iteratee 包(坦率地说,它缺少很多部分,并且使用起来有点痛
我想弄清楚如何使用 StateT结合两个 State基于对我的 Scalaz state monad examples 的评论的状态转换器回答。 看来我已经很接近了,但是在尝试申请 sequence
我正在寻找一种用于异步操作的数据类型。 我发现 scalaz.ContT[Trampoline, Unit, ?]支持 scalaz.concurrent.Future 中的所有功能,此外还有 Bin
为什么我会收到以下信息 error: could not find implicit value for parameter C: scalaz.Catchable[F2]执行P(1,2,3).run
我在我的代码中看到了一个常见的模式。我已经对数据库中的结果进行了排序,我需要以嵌套结构发出它们。我希望它可以流式传输,因此我希望一次在内存中保留尽可能少的记录。使用 TravesableLike.gr
阅读http://eed3si9n.com/learning-scalaz/Tagged+type.html并尝试示例代码: import scalaz._; import Scalaz._ seal
我如何转换 val from: ValidationNel[E, ValidationNel[E, T]] 到 val to: ValidationNel[E, T] 同时捕获所有验证错误? 最佳答案
我试图在我们的项目中使用 scalaz 验证,但遇到了以下情况: def rate(username: String, params: Map[String, String]): Validation
在 scalaz 中,当我们定义一个模块时,我们额外定义了隐式的辅助函数。这是一个定义示例以及客户如何使用它: trait Functor[F[_]] { def map[A,B](fa: F[A
有一个trait叫 克莱斯利 在 scalaz图书馆。查看代码: import scalaz._ import Scalaz._ type StringPair = (String, String)
你们知道为什么 Scalaz 的例子总是使用这种导入技术: import scalaz._ import Scalaz._ 而不是: import scalaz.Scalaz._ ?我试图了解偏好背后
这是一个非常简单的问题。观看精彩的镜头介绍后: http://www.youtube.com/watch?v=efv0SQNde5Q 我想我可以尝试演讲中介绍的简单示例之一: import scala
如果我有多个返回具有固定错误类型的东西的 Validation[E, _] 的操作,我可以在 for-comprehension 中使用它们。例如: val things: Validation[E,
我使用 bing 操作在 Scalaz 上编写了一个 Fibonacci 函数。这是我的代码: import scalaz._, Scalaz._ def fib(i: Int): Option[In
我是一名优秀的程序员,十分优秀!