Skip to content

Instantly share code, notes, and snippets.

@drstevens
Created April 19, 2012 20:42
Show Gist options
  • Save drstevens/2424008 to your computer and use it in GitHub Desktop.
Save drstevens/2424008 to your computer and use it in GitHub Desktop.
Convert List[Validation[A, B]] to Validation[List[A], List[B]]
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