Bryan Cosgrove
Bryan Cosgrove

Reputation: 1393

Scalaz use applicative builder with validations and a list of validations

I'm working with Scalaz validations and I've run into a situation like this (note this is heavily simplified from my actual code, but the idea is the same)

Given:

case class Foo(bar: Int)

val x1: Validation[String, Foo] = Foo(1).success
val x2: Validation[String, Foo] = Foo(2).success
val x3: Validation[String, Foo] = Foo(3).success

val l1 = List(x1, x2)

I would like to be able to do something along the lines this:

(x3 |@| l1) { (x1, x2, x3) => /*do something with all of my Foo's*/ } 

Of course if there were any errors, either in the list or outside of the list I'd like them to accumulate as they normally would.

I know the above syntax does not work, but any advice on how to achieve the result I'm looking for would be appreciated.

Upvotes: 1

Views: 190

Answers (1)

Travis Brown
Travis Brown

Reputation: 139038

If you have a List[F[A]] and F has an applicative functor instance, you can turn the list inside out in effect with sequenceU to get a F[List[A]]:

scala> l1.sequenceU
res0: scalaz.Validation[String,List[Foo]] = Success(List(Foo(1), Foo(2)))

Or:

scala> (x3 |@| l1.sequenceU) {
  case (third, rest) => // do something with the values
}

It's also worth noting that if you find yourself writing things of the xs.map(f).sequenceU, you can use xs.traverseU(f) instead—it's exactly equivalent except you don't build an intermediate list.

Upvotes: 2

Related Questions