0

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. I have validated and I want to process them sequentially.

Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
  • what you're trying to do sounds more like you were looking for the applicative functor. Here's an example with cats/validated: http://eed3si9n.com/herding-cats/Validated.html – rethab Apr 20 '17 at 15:02
  • No I don't want all the errors. I have validated, but this time I want to process sequentially. – Knows Not Much Apr 20 '17 at 15:07

1 Answers1

0

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)
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373