Created
April 19, 2012 20:42
-
-
Save drstevens/2424008 to your computer and use it in GitHub Desktop.
Convert List[Validation[A, B]] to Validation[List[A], List[B]]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mypackage | |
import org.specs.SpecificationWithJUnit | |
import scalaz._ | |
import Scalaz._ | |
class ValidationTests extends SpecificationWithJUnit { | |
"I should be able to convert List[Validation[A, B]] to Validation[List[A], List[B]]" >> { | |
def convert[A, B](l: List[Validation[A, B]]): Validation[NonEmptyList[A], List[B]] = | |
l.traverse[({type l[a] = ValidationNEL[A, a]})#l, B](_.liftFailNel) | |
val someFailures: List[Validation[String, Int]] = List("a".fail, 1.success, "b".fail, 2.success) | |
convert(someFailures).fail.map(_.list).validation must be equalTo (List("a", "b").fail) | |
val allSuccess: List[Validation[String, Int]] = (0 until 4).map(_.success).toList | |
convert(allSuccess) must be equalTo ((0 until 4).toList.success) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment