Validation Scalaz:在用于理解和日志记录的
我承认标题不是很明确:对不起 假设我有理解的能力:Validation Scalaz:在用于理解和日志记录的,validation,logging,scalaz,Validation,Logging,Scalaz,我承认标题不是很明确:对不起 假设我有理解的能力: for {v1<-Validation1(input) v2<-Validation2(v1) v3<-Validation3(v2) } yield result 但我找不到如何将它们结合在一起以便于理解。 此外,为了得到其中一个的结果,我必须写: squareroot2(4).run.run 这似乎很奇怪 按照我写的方式,即使失败,字符串successsquareroot也会被记录: println
for {v1<-Validation1(input)
v2<-Validation2(v1)
v3<-Validation3(v2)
} yield result
但我找不到如何将它们结合在一起以便于理解。
此外,为了得到其中一个的结果,我必须写:
squareroot2(4).run.run
这似乎很奇怪
按照我写的方式,即使失败,字符串successsquareroot也会被记录:
println(squareroot2(-1).run.run)
打印:(平方根正常,失败(不能取负数的平方根))
谢谢大家!!
贝诺特
于4月12日编辑
所以你们八个建议了这个片段:
def squareroot(x:Double) = if (x < 0) failureT("Can't take squareroot of negative number") else successT(sqrt(x))
def inverse(x:Double) = if (x == 0) failureT("Can't take inverse of zero ") else successT(1/x)
for {
y <- squareroot(x).flatMapF(i => Writer("Squareroot ok", i))
z <- inverse(y).flatMapF(i => Writer("Inverse ok", i))
} yield z
酷!我最好为作者使用一个列表[字符串],但我认为我已经走上了好的道路
现在,我可以想到我的假期(明天!):)
于5月14日编辑
嗯,代码没有编译,但是错误在Yo-Eight的最后一个建议中(请注意,Yo-Eight是善良的典范,这又不是冒犯!)。我向您提交完整代码和错误:
import scala.math._
import scalaz._
import Scalaz._
object validlog extends ValidationTFunctions {
val failsquareroot= "Can't take squareroot of negative number"
val successsquareroot= "Squareroot ok"
val failinverse="Can't take inverse of zero"
val successinverse= "Inverse ok"
case class MyId[A]( v: A)
implicit val myIdPointed = new Pointed[MyId]{
def point[A](v: => A) = MyId(v)
}
implicit def unId[A](my: MyId[A]): A = my.v
def squareroot(x:Double):ValidationT[({type f[x] = WriterT[MyId,String, x]})#f,String,Double]=if (x < 0) failureT[({type f[x] = WriterT[MyId,String, x]})#f,String,Double](failsquareroot) else successT[({type f[x] = WriterT[MyId,String, x]})#f,String,Double](sqrt(x))
def inverse(x:Double):ValidationT[({type f[x] = WriterT[MyId, String, x]})#f,String,Double]=if (x == 0) failureT[({type f[x] = WriterT[MyId,String, x]})#f,String,Double](failinverse) else successT[({type f[x] = WriterT[MyId,String, x]})#f,String,Double](1/x)
/* def resultat(x:Double) = for {
y <- squareroot(x).flatMapF(i => Writer(", Squareroot ok", i))
z <- inverse(y).flatMapF(i => Writer(", Inverse ok", i))
} yield z */
def main(args: Array[String]): Unit = {
println(inverse(0.0).run)
println(inverse(0.5).run)
println(squareroot(-1.0).run)
println(inverse(4.0).run)
}
}
我想我从一开始就错过了一些东西,这可以解释为什么我会被粘上几个星期
贝诺特
于5月15日编辑
在编译代码时,我遇到了第一个错误:
could not find implicit value for parameter F: scalaz.Pointed[Main.$anon.ValidationTExample.WriterAlias]
经过一些尝试,我以以下方式重写了导入:
import scalaz.Writer
import scalaz.std.string._
import scalaz.Id._
import scalaz.WriterT
import scalaz.ValidationT
import scala.Math._
还有一个错误:
error: could not find implicit value for parameter F: scalaz.Monad[[x]scalaz.WriterT[[+X]X,String,x]]
y <- squareroot(x).flatMapF(i => Writer("Squareroot ok", i))
^
one error found
哟八,如果有一天我们碰巧见面,我会付给你一杯啤酒
Benoit为了在一元计算期间进行日志记录,必须使用Writer monad的实例。由于monad不组合,并且您希望保持“验证”效果,所以应该使用验证monad转换器。我不知道您使用的是哪个版本的ScalaZ,但是Scalaz7(分支Scalaz7)提供了这样的monad转换器(即ValidationT) 因此,我们得到:
ValidationT[({type f[x] = Writer[W, x]})#f, A]
使用您的记录器的类型
根据您的编辑,我将这样做
def squareroot(x:Double) = if (x < 0) failureT("Can't take squareroot of negative number") else successT(sqrt(x))
def inverse(x:Double) = if (x == 0) failureT("Can't take inverse of zero ") else successT(1/x)
这样,您可以像这样定义resultat方法:
def resultat(x:Double) = for {
y <- squareroot(x).flatMapF(i => Writer(", Squareroot ok", i))
z <- inverse(y).flatMapF(i => Writer(", Inverse ok", i))
} yield z
因为WriterT需要一个Monoid[String]和Pointed[Id]的实例
import std.string._ // this import all string functions and instances
import Id._ // this import all Id functions and instances
下面是完整的可执行代码
import scalaz._
import std.string._
import Id._
import scalaz.WriterT
import scalaz.ValidationT
import scala.Math._
object ValidationTExample extends Application {
type ValidationTWriterAlias[W, A] = ValidationT[({type f[x] = Writer[W, x]})#f, W, A]
type WriterAlias[A] = Writer[String, A]
def squareroot(x:Double): ValidationTWriterAlias[String, Double] =
if (x < 0) ValidationT.failureT[WriterAlias, String, Double]("Can't take squareroot of negative number")
else ValidationT.successT[WriterAlias, String, Double](sqrt(x))
def inverse(x:Double): ValidationTWriterAlias[String, Double] =
if (x == 0) ValidationT.failureT[WriterAlias, String, Double]("Can't take inverse of zero ")
else ValidationT.successT[WriterAlias, String, Double](1/x)
def resultat(x:Double) = for {
y <- squareroot(x).flatMapF(i => Writer("Squareroot ok", i))
z <- inverse(y).flatMapF(i => Writer(", Inverse ok", i))
} yield z
println("0 : " + resultat(0.0).run.run)
println("-1 : " + resultat(-1.0).run.run)
println("4 : " + resultat(4).run.run)
}
导入scalaz_
导入标准字符串_
导入Id_
导入scalaz.WriterT
导入scalaz.ValidationT
导入scala.Math_
对象验证示例扩展了应用程序{
类型ValidationTWriterAlias[W,A]=ValidationT[({type f[x]=Writer[W,x]})#f,W,A]
类型WriterAlias[A]=Writer[String,A]
def平方根(x:Double):ValidationTWriterAlias[String,Double]=
if(x<0)ValidationT.failureT[writerias,String,Double](“不能取负数的平方根”)
else ValidationT.successT[writerias,String,Double](sqrt(x))
def inverse(x:Double):validationtwriteraas[String,Double]=
if(x==0)ValidationT.failureT[WriterAlias,String,Double](“不能取零的倒数”)
else ValidationT.successT[writerias,String,Double](1/x)
def resultat(x:Double)=用于{
y Writer(“平方根ok”,i))
z书写器(“,逆ok”,i))
}屈服强度z
println(“0:+resultat(0.0).run.run)
println(“-1:+resultat(-1.0).run.run)
println(“4:+resultat(4.run.run)
}
8月14日编辑
此代码在scalaz seven中不再有效。ValidationT已被删除,因为Validation不是monad。希望可以用它来代替。此外,还添加了一个新的MonadWriter/ListenableMonadWriter类型类来减轻这些类型注释
import scalaz._
import std.string._
import syntax.monadwriter._
import scala.Math._
object EitherTExample extends Application {
implicit val monadWriter = EitherT.monadWriter[Writer, String, String]
def squareroot(x: Double) =
if (x < 0)
monadWriter.left[Double]("Can't take squareroot of negative number")
else
monadWriter.right[Double](sqrt(x))
def inverse(x: Double) =
if (x == 0)
monadWriter.left[Double]("Can't take inverse of zero")
else
monadWriter.right[Double](1 / x)
def resultat(x: Double) = for {
y <- squareroot(x) :++> "Squareroot ok"
z <- inverse(y) :++> ", Inverse ok"
} yield z
println("0 : " + resultat(0.0).run.run)
println("-1 : " + resultat(-1.0).run.run)
println("4 : " + resultat(4).run.run)
}
导入scalaz_
导入标准字符串_
导入syntax.monadwriter_
导入scala.Math_
对象实例扩展了应用程序{
隐式val monadWriter=EitherT.monadWriter[Writer,String,String]
def平方根(x:Double)=
if(x<0)
monadWriter.left[Double](“不能取负数的平方根”)
其他的
单子书写器右[双](sqrt(x))
def反向(x:Double)=
如果(x==0)
monadWriter.left[Double](“不能取零的倒数”)
其他的
单子写入器右[双](1/x)
def resultat(x:Double)=用于{
y“平方根ok”
z“,逆ok”
}屈服强度z
println(“0:+resultat(0.0).run.run)
println(“-1:+resultat(-1.0).run.run)
println(“4:+resultat(4.run.run)
}
谢谢你,我用的是Scalaz 6.0.4,我会检查它提供了什么。不管怎样,我理解这个想法。我被粘住了!如果我将Validation1、Validation2和Validation3替换为ValidationT[…](Writer(“blabla”,Validation1(x))等),我成功地链接了3个验证,但我看不到如何累积日志…调用Writer.flatMap时,日志隐式地“累积”(需要一个Monoid[W]实例)调用ValidationT.flatMap时,会隐式调用.Writer.flatMap。谢谢你,Yo-eight,但我仍然有麻烦…我将编辑我的问题,以显示我在做什么。再次感谢Yo-eight,但我放弃了一段时间…我想在我感到轻松之前,我必须在scalaz、monads Transformer等方面进行更深入的研究你的scalaz seven版本不好。代码运行正确,最后一个版本的scalaz seven branchI错过了你的帖子。这是我猜测的。我正在使用4月14日的快照。今晚我应该有时间测试最后一个版本
0 : (Squareroot ok,Failure(Can't take inverse of zero ))
-1 : (,Failure(Can't take squareroot of negative number))
4 : (Squareroot ok, Inverse ok,Success(0.5))
ValidationT[({type f[x] = Writer[W, x]})#f, A]
def squareroot(x:Double) = if (x < 0) failureT("Can't take squareroot of negative number") else successT(sqrt(x))
def inverse(x:Double) = if (x == 0) failureT("Can't take inverse of zero ") else successT(1/x)
for {
y <- squareroot(x).flatMapF(i => Writer("Squareroot ok", i))
z <- inverse(y).flatMapF(i => Writer("Inverse ok", i))
} yield z
def squareroot(x:Double):ValidationT[({type f[x] = Writer[String, x]})#f,String,Double]
def inverse(x:Double):ValidationT[{type f[x] = Writer[String, x]})#f,String,Double]
def resultat(x:Double) = for {
y <- squareroot(x).flatMapF(i => Writer(", Squareroot ok", i))
z <- inverse(y).flatMapF(i => Writer(", Inverse ok", i))
} yield z
implicitly[Pointed[({ type f[x] = Writer[String, x] })#f]]
import std.string._ // this import all string functions and instances
import Id._ // this import all Id functions and instances
import scalaz._
import std.string._
import Id._
import scalaz.WriterT
import scalaz.ValidationT
import scala.Math._
object ValidationTExample extends Application {
type ValidationTWriterAlias[W, A] = ValidationT[({type f[x] = Writer[W, x]})#f, W, A]
type WriterAlias[A] = Writer[String, A]
def squareroot(x:Double): ValidationTWriterAlias[String, Double] =
if (x < 0) ValidationT.failureT[WriterAlias, String, Double]("Can't take squareroot of negative number")
else ValidationT.successT[WriterAlias, String, Double](sqrt(x))
def inverse(x:Double): ValidationTWriterAlias[String, Double] =
if (x == 0) ValidationT.failureT[WriterAlias, String, Double]("Can't take inverse of zero ")
else ValidationT.successT[WriterAlias, String, Double](1/x)
def resultat(x:Double) = for {
y <- squareroot(x).flatMapF(i => Writer("Squareroot ok", i))
z <- inverse(y).flatMapF(i => Writer(", Inverse ok", i))
} yield z
println("0 : " + resultat(0.0).run.run)
println("-1 : " + resultat(-1.0).run.run)
println("4 : " + resultat(4).run.run)
}
import scalaz._
import std.string._
import syntax.monadwriter._
import scala.Math._
object EitherTExample extends Application {
implicit val monadWriter = EitherT.monadWriter[Writer, String, String]
def squareroot(x: Double) =
if (x < 0)
monadWriter.left[Double]("Can't take squareroot of negative number")
else
monadWriter.right[Double](sqrt(x))
def inverse(x: Double) =
if (x == 0)
monadWriter.left[Double]("Can't take inverse of zero")
else
monadWriter.right[Double](1 / x)
def resultat(x: Double) = for {
y <- squareroot(x) :++> "Squareroot ok"
z <- inverse(y) :++> ", Inverse ok"
} yield z
println("0 : " + resultat(0.0).run.run)
println("-1 : " + resultat(-1.0).run.run)
println("4 : " + resultat(4).run.run)
}