-
Validatedscala/cats2 2023. 1. 19. 21:36
Validated 예제
import cats.data._ import cats.syntax.all._ object ValidatedTest { def main(args: Array[String]): Unit = { val x: ValidatedNel[String, Int] = 1.validNel[String] // 1.valid[NonEmptyList[String]] val y: ValidatedNel[String, Int] = 2.validNel[String] // 2.valid[NonEmptyList[String]] val e1: ValidatedNel[String, Int] = "error1".invalidNel[Int] // NonEmptyList.of("error1").invalid[Int] val e2: ValidatedNel[String, Int] = "error2".invalidNel[Int] // NonEmptyList.of("error2").invalid[Int] val validResult: Validated[NonEmptyList[String], Int] = x.combine(y) // Valid(3) val validResult2: ValidatedNel[String, Int] = (x, y).mapN(_ + _) // Valid(3) val invalidResult: ValidatedNel[String, Int] = x |+| y |+| e1 |+| e2 // Invalid(NonEmptyList(error1, error2)) val invalidResult2: ValidatedNel[String, Int] = List(x, y, e1, e2).reduce(_ |+| _) // Invalid(NonEmptyList(error1, error2)) } }