Convert List[ValidationNEL[String, Unit]] to ValidationNEL[String, Unit]
Question
Is there a standard way to flatten the left sides into a single list?
thanks!
Solution
I'm not exactly sure what you're trying to do, but the Monoid
instance should do the trick:
scala> List(
| println("test").successNel[String],
| "a".failNel[Unit],
| "b".failNel[Unit]
| ).suml == Failure(NonEmptyList("a", "b"))
test
res0: Boolean = true
Where suml
is a method that's pimped onto any collection full of something with a Monoid
instance.
OTHER TIPS
Ok, here is some code that does the trick:
scala> type MyValidationNEL[A] = ValidationNEL[String, A]
defined type alias MyValidationNEL
scala> val x: List[MyValidationNEL[Unit]] =
| List("a".failNel, "b".failNel)
x: List[MyValidationNEL[Unit]] = List(Failure(NonEmptyList(a)), Failure(NonEmptyList(b)))
scala> x.sequence
res57: MyValidationNEL[List[Unit]] = Failure(NonEmptyList(a, b))
scala>
I've taken part of the example from this presentation: http://www.scribd.com/doc/93526802/Bjarnason-Scalaz by Rúnar Bjarnason http://scalaz.org
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow