scala - Scalaz use applicative builder with validations and a list of validations -
i'm working scalaz validations , i've run situation (note heavily simplified actual code, idea 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 able along lines this:
(x3 |@| l1) { (x1, x2, x3) => /*do of foo's*/ }
of course if there errors, either in list or outside of list i'd them accumulate would.
i know above syntax not work, advice on how achieve result i'm looking appreciated.
if have list[f[a]]
, f
has applicative functor instance, can turn list inside out in effect sequenceu
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) => // values }
it's worth noting if find writing things of xs.map(f).sequenceu
, can use xs.traverseu(f)
instead—it's equivalent except don't build intermediate list.
Comments
Post a Comment