scalaz 7.2.6 flatMap 不是验证方法?
scalaz 7.2.6 flatMap is not a method of Validation?
这适用于 scalaz 7.0.6,但不适用于最新版本的 scalaz 7.2.6。
import scalaz._, Scalaz._
def allDigits(s: String): Validation[String, String] =
if (s.forall(_.isDigit)) s.success else "Not all digits".failure
def maxSizeOfTen(s: String): Validation[String, String] =
if (s.length <= 10) s.success else "Too big".failure
def toInt(s: String) = try s.toInt.success catch {
case _: NumberFormatException => "Still not an integer".failure
}
val validated1 = for {
x <- allDigits("4234")
y <- maxSizeOfTen(x)
z <- toInt(y)
} yield z
我在 scalaz 7.2.6 上遇到这些错误:
value flatMap is not a member of scalaz.Validation[String,String]
x <- allDigits("4234")
value flatMap is not a member of scalaz.Validation[String,String]
y <- maxSizeOfTen(x)
...
如何让它在最新版本的 scalaz 上运行?
更新:基于已接受答案的解决方案:
import scalaz._, Scalaz._
def allDigits(s: String): \/[String, String] =
if (s.forall(_.isDigit)) s.right else "Not all digits".left
def maxSizeOfTen(s: String): \/[String, String] =
if (s.length <= 10) s.right else "Too big".left
def toInt(s: String) = try s.toInt.right catch {
case _: NumberFormatException => "Still not an integer".left
}
val validated1 = for {
x <- allDigits("4234")
y <- maxSizeOfTen(x)
z <- toInt(y)
} yield z
验证不应与 flatMap
一起使用,因为它旨在累积失败,因此具有 Applicative
独立(无上下文)计算的实例。 \/
应该用于您的情况(依赖(或上下文相关)计算)。
不过,通过添加此导入,您可以实现您想要的:
import Validation.FlatMap._
正如前面的回答所指出的,当您想要并行 运行 事物并立即获取所有错误时,验证很有用。
适用方法:
def allDigits(s: String): Validation[String, String] =
if (s.forall(_.isDigit)) s.success else s"|error: '$s' Not all digits ".failure
def maxSizeOfTen(s: String): Validation[String, String] =
if (s.length <= 10) s.success else s"|error: '$s' Too big".failure
def toInt(s: String) = try s.toInt.success catch {
case _: NumberFormatException => "|Still not an integer".failure
}
val input = "4234"
val validated1 = (allDigits(input) |@| maxSizeOfTen(input)) { (x, _) => toInt(x) }
println(validated1)
val input2 = "123456789ten"
val validated2 = (allDigits(input2) |@| maxSizeOfTen(input2)) { (x, _) => toInt(x) }
println(validated2)
你将有下一个输出:
Success(Success(4234))
Failure(|error: '123456789ten' Not all digits |error: '123456789ten' Too big)
这适用于 scalaz 7.0.6,但不适用于最新版本的 scalaz 7.2.6。
import scalaz._, Scalaz._
def allDigits(s: String): Validation[String, String] =
if (s.forall(_.isDigit)) s.success else "Not all digits".failure
def maxSizeOfTen(s: String): Validation[String, String] =
if (s.length <= 10) s.success else "Too big".failure
def toInt(s: String) = try s.toInt.success catch {
case _: NumberFormatException => "Still not an integer".failure
}
val validated1 = for {
x <- allDigits("4234")
y <- maxSizeOfTen(x)
z <- toInt(y)
} yield z
我在 scalaz 7.2.6 上遇到这些错误:
value flatMap is not a member of scalaz.Validation[String,String]
x <- allDigits("4234")
value flatMap is not a member of scalaz.Validation[String,String]
y <- maxSizeOfTen(x)
...
如何让它在最新版本的 scalaz 上运行?
更新:基于已接受答案的解决方案:
import scalaz._, Scalaz._
def allDigits(s: String): \/[String, String] =
if (s.forall(_.isDigit)) s.right else "Not all digits".left
def maxSizeOfTen(s: String): \/[String, String] =
if (s.length <= 10) s.right else "Too big".left
def toInt(s: String) = try s.toInt.right catch {
case _: NumberFormatException => "Still not an integer".left
}
val validated1 = for {
x <- allDigits("4234")
y <- maxSizeOfTen(x)
z <- toInt(y)
} yield z
验证不应与 flatMap
一起使用,因为它旨在累积失败,因此具有 Applicative
独立(无上下文)计算的实例。 \/
应该用于您的情况(依赖(或上下文相关)计算)。
不过,通过添加此导入,您可以实现您想要的:
import Validation.FlatMap._
正如前面的回答所指出的,当您想要并行 运行 事物并立即获取所有错误时,验证很有用。
适用方法:
def allDigits(s: String): Validation[String, String] =
if (s.forall(_.isDigit)) s.success else s"|error: '$s' Not all digits ".failure
def maxSizeOfTen(s: String): Validation[String, String] =
if (s.length <= 10) s.success else s"|error: '$s' Too big".failure
def toInt(s: String) = try s.toInt.success catch {
case _: NumberFormatException => "|Still not an integer".failure
}
val input = "4234"
val validated1 = (allDigits(input) |@| maxSizeOfTen(input)) { (x, _) => toInt(x) }
println(validated1)
val input2 = "123456789ten"
val validated2 = (allDigits(input2) |@| maxSizeOfTen(input2)) { (x, _) => toInt(x) }
println(validated2)
你将有下一个输出:
Success(Success(4234))
Failure(|error: '123456789ten' Not all digits |error: '123456789ten' Too big)