Created
May 29, 2012 21:29
-
-
Save jrwest/2830838 to your computer and use it in GitHub Desktop.
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
scala> type Validated[X] = Option[X] | |
defined type alias Validated | |
scala> badData.traverse[Validated,Int](i => if (i < 0) none[Int] else i.point[Validated]) | |
res3: Validated[List[Int]] = None | |
scala> goodData.traverse[Validated,Int](i => if (i < 0) none[Int] else i.point[Validated]) | |
res4: Validated[List[Int]] = Some(List(4, 5, 123, 6, 7)) | |
scala> import scalaz.syntax.validation._; import scalaz.Validation; import Validation._; import scalaz.std.string._ | |
... | |
scala> type Validated[X] = Validation[String,X] | |
defined type alias Validated | |
scala> badData.traverse[Validated,Int](i => if (i < 0) "invalid data".fail else i.point[Validated]) | |
res10: Validated[List[Int]] = Failure(invalid data) | |
scala> goodData.traverse[Validated,Int](i => if (i < 0) "invalid data".fail else i.point[Validated]) | |
res11: Validated[List[Int]] = Success(List(4, 5, 123, 6, 7)) | |
scala> import scalaz.ValidationNEL | |
import scalaz.ValidationNEL | |
scala> type Validated[X] = ValidationNEL[Int,X] | |
defined type alias Validated | |
scala> badData.traverse[Validated,Int](i => if (i < 0) i.failNel else i.point[Validated]) | |
res16: Validated[List[Int]] = Failure(NonEmptyList(-1)) | |
scala> goodData.traverse[Validated,Int](i => if (i < 0) i.failNel else i.point[Validated]) | |
res17: Validated[List[Int]] = Success(List(4, 5, 123, 6, 7)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment