假设我有:
val m: Map[String, Int] = Map("one" -> 1, "five" -> 5, "six" -> 6, "nine" -> 9)
我有两个功能:
def isNotDivisibleByTwo(i: Int): ValidatedNec[String, Int] = Validated.condNec(i%2!=0, i, s"$i is divisible by 2.")
def isNotDivisibleByThree(i: Int): ValidatedNec[String, Int] = Validated.condNec(i%3!=0, i, s"$i is divisible by 3.")
我想要一个函数,它能给我:
def sanitize(m: Map[String, Int]):Map[String, Validated[NonEmptyList[String], Int]] = ???
即,它应该返回满足上述两个函数的所有数字,以及所有故障数字及其相关故障的映射。
例如,对于给定的列表 m,我想得到:
val result = Map(
"one" -> Valid(1),
"five -> Valid(5),
"nine" -> Invalid(NonEmptyList("9 is dividible by 3")),
"six" -> Invalid(NonEmptyList("6 is dividible by 2", "6 is dividible by 3"))
)
这是我目前拥有的:
import cats.data._
val m: Map[String, Int] = Map("one" -> 1, "five" -> 5, "six" -> 6, "nine" -> 9)
def isNotDivisibleByTwo(i: Int): ValidatedNec[String, Unit] = Validated.condNec(i%2!=0, (), s"$i is divisible by 2.")
def isNotDivisibleByThree(i: Int): ValidatedNec[String, Unit] = Validated.condNec(i%3!=0, (), s"$i is divisible by 3.")
def sanitize(m: Map[String, Int]): Map[String, Validated[NonEmptyChain[String], Int]] = {
m.mapValues{
i =>
isNotDivisibleByTwo(i).product(
isNotDivisibleByThree(i)
).map(_ => i)
}
}
但是,我对"撰写"验证的方式不满意。
我怎样才能以最猫的方式做到这一点?
你们离得太近了。
请记住,组合多个验证的正确方法是使用Applicative
语法。
import cats.data.{Validated, ValidatedNec}
import cats.syntax.apply._
type ErrorsOr[A] = ValidatedNec[String, A]
def isNotDivisibleByTwo(i: Int): ErrorsOr[Int] =
Validated.condNec((i % 2) != 0, i, s"$i is divisible by 2.")
def isNotDivisibleByThree(i: Int): ErrorsOr[Int] =
Validated.condNec((i % 3) != 0, i, s"$i is divisible by 3.")
val map: Map[String, Int] = Map("one" -> 1, "five" -> 5, "six" -> 6, "nine" -> 9)
def sanitize(m: Map[String, Int]): Map[String, ErrorsOr[Int]] =
m.view.mapValues { i =>
(
isNotDivisibleByTwo(i),
isNotDivisibleByThree(i)
).tupled.map(_ => i)
}.toMap
sanitize(map)
// res: Map[String, ErrorsOr[Int]] = Map(
// "one" -> Valid(1),
// "five" -> Valid(5),
// "six" -> Invalid(Append(Singleton("6 is divisible by 2."), Singleton("6 is divisible by 3."))),
// "nine" -> Invalid(Singleton("9 is divisible by 3."))
// )
但是,您可以使代码更加通用,以使用任意数量的验证。通过使用traverse
.
(在这种情况下,您不需要任何语法导入(。
import cats.data.NonEmptyList
val validations: NonEmptyList[Int => ErrorsOr[Int]] = NonEmptyList.of(isNotDivisibleByTwo, isNotDivisibleByThree)
def sanitize[K, V](map: Map[K, V])
(validations: NonEmptyList[V => ErrorsOr[V]]): Map[K, ErrorsOr[V]] =
map.view.mapValues(i => validations.traverse(f => f(i)).map(_ => i)).toMap
sanitize(map)(validations)
// res: Map[String, ErrorsOr[Int]] = Map(
// "one" -> Valid(1),
// "five" -> Valid(5),
// "six" -> Invalid(Append(Singleton("6 is divisible by 2."), Singleton("6 is divisible by 3."))),
// "nine" -> Invalid(Singleton("9 is divisible by 3."))
// )
我之所以使用.view.mapValues(...).toMap
是因为在Scala 2.13上mapValues
已被弃用。