作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
斯卡拉社区。
目前我正在尝试使用猫实现自定义模型/单参数验证 Validated
单子(monad)。但是,自 1.0 以来删除笛卡尔积后,我无法使用 (v1 |@| v2) 映射 (f) 并且无法编译我的代码:
import cats.Semigroupal
import cats.data.Validated.{Invalid, Valid}
import cats.data.{ValidatedNel, _}
import cats.implicits._
import cats.instances.all._
case class FieldErrorInfo(name: String, error: String)
type FieldName = String
type ValidationResult[A] = ValidatedNel[FieldErrorInfo, A]
trait SingleFieldValidationRule[U] extends ((U, FieldName) => ValidationResult[U])
trait ModelValidationRule[M] extends (M => ValidationResult[M])
object ValidateNameRule extends SingleFieldValidationRule[String] {
override def apply(v1: String, name: String): ValidationResult[String] = {
if (v1.contains("cats"))
v1.validNel
else
FieldErrorInfo(name, "Some Error").invalidNel
}
}
object ValidateQuantityRule extends SingleFieldValidationRule[Int] {
override def apply(v1: Int, name: String): ValidationResult[Int] =
if (v1 > 0)
v1.validNel
else FieldErrorInfo(name, "Some Error").invalidNel
}
case class SampleModel(name: String, quantity: Int)
object ValidateSampleModel extends ModelValidationRule[SampleModel] {
override def apply(v1: SampleModel): ValidationResult[SampleModel] = {
val stage1: ValidatedNel[FieldErrorInfo, String] = ValidateNameRule(v1.name, "name")
val stage2: ValidatedNel[FieldErrorInfo, Int] = ValidateQuantityRule(v1.quantity, "quantity")
implicit val sga: Semigroupal[NonEmptyList] = new Semigroupal[NonEmptyList] {
override def product[A, B](fa: NonEmptyList[A], fb: NonEmptyList[B]): NonEmptyList[(A, B)] = fa.flatMap(a => fb.map(b => a -> b))
}
(stage1, stage2).mapN(SampleModel)
}
}
Error:(43, 23) value mapN is not a member of (cats.data.ValidatedNel[FieldErrorInfo,String], cats.data.ValidatedNel[FieldErrorInfo,Int])
(stage1, stage2).mapN(SampleModel)
^
最佳答案
您似乎缺少以下导入:
import cats.syntax.apply._
mapN
.
-Ypartial-unification
编译器标志已激活,否则编译器将难以提取
ValidatedNel[FieldErrorInfo, ?]
来自
stage1
的类型和
stage2
:
libraryDependencies += "org.typelevel" %% "cats-core" % "1.1.0"
scalaVersion := "2.12.5"
scalacOptions += "-Ypartial-unification"
import cats.Semigroupal
import cats.data.Validated.{Invalid, Valid}
import cats.data.ValidatedNel
import cats.data.NonEmptyList
import cats.syntax.apply._ // for `mapN`
import cats.syntax.validated._ // for `validNel`
case class FieldErrorInfo(name: String, error: String)
type FieldName = String
type ValidationResult[A] = ValidatedNel[FieldErrorInfo, A]
trait SingleFieldValidationRule[U] extends ((U, FieldName) => ValidationResult[U])
trait ModelValidationRule[M] extends (M => ValidationResult[M])
object ValidateNameRule extends SingleFieldValidationRule[String] {
override def apply(v1: String, name: String): ValidationResult[String] = {
if (v1.contains("cats"))
v1.validNel
else
FieldErrorInfo(name, "Some Error").invalidNel
}
}
object ValidateQuantityRule extends SingleFieldValidationRule[Int] {
override def apply(v1: Int, name: String): ValidationResult[Int] =
if (v1 > 0)
v1.validNel
else FieldErrorInfo(name, "Some Error").invalidNel
}
case class SampleModel(name: String, quantity: Int)
object ValidateSampleModel extends ModelValidationRule[SampleModel] {
override def apply(v1: SampleModel): ValidationResult[SampleModel] = {
val stage1: ValidatedNel[FieldErrorInfo, String] = ValidateNameRule(v1.name, "name")
val stage2: ValidatedNel[FieldErrorInfo, Int] = ValidateQuantityRule(v1.quantity, "quantity")
implicit val sga: Semigroupal[NonEmptyList] = new Semigroupal[NonEmptyList] {
override def product[A, B](fa: NonEmptyList[A], fb: NonEmptyList[B]): NonEmptyList[(A, B)] = fa.flatMap(a => fb.map(b => a -> b))
}
(stage1, stage2).mapN(SampleModel)
}
}
关于Scala-Cats 已验证 : value mapN is not a member of ValidatedNel tuple,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49865936/
鉴于: def convert[T](list: List[Either[String, T]]): Validated[NonEmptyList[String], NonEmptyList[T]]
我有以下代码 sealed trait DomainValidation { def errorMessage: String } type ValidationResult[A] = Valid
我有这个: Set[ValidatedNel[String, Double]] 我想将其中的 double 相加得到: ValidatedNel[String, Double] 如果值中的某些元素是,
斯卡拉社区。 目前我正在尝试使用猫实现自定义模型/单参数验证 Validated单子(monad)。但是,自 1.0 以来删除笛卡尔积后,我无法使用 (v1 |@| v2) 映射 (f) 并且无法编译
我是一名优秀的程序员,十分优秀!