I am reading this article
http://typelevel.org/cats/datatypes/validated.html
It says that Validated can be used to do Sequential validations using "andThen" method. This means we stop at the first error and not collect all the errors.
I tried the following code
@ val x = 123.valid[String]
x: Validated[String, Int] = Valid(123)
@ val y = "foo".invalid[Int]
y: Validated[String, Int] = Invalid("foo")
@ x andThen y
cmd4.sc:1: type mismatch;
found : cats.data.Validated[String,Int]
required: Int => cats.data.Validated[?,?]
val res4 = x andThen y
^
Buy why the type mismatch. As you can see both x and y have the same shape.
Edit: Note that I don't want to collect all errors. That I could do easily with x |@| y
x |@| y
. I have validated and I want to process them sequentially.
OK. I think I found the answer by looking here . To chain Validation monads using andthen method. You need a function which takes the right side of the first Validated, and then creates another validated.
so the right code would be
@ val x = 123.valid[String]
val y = x: Validated[String, Int] = Valid(123)
@ val y = 234.valid[String]
y: Validated[String, Int] = Valid(234)
@ val foo = (i: Int) => y
foo: Int => Validated[String, Int] = $sess.cmd4$$$Lambda$2259/7469297@25f7739c
@ x andThen foo
res5: Validated[String, Int] = Valid(234)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.