Created
February 3, 2015 21:56
-
-
Save justjoheinz/77f9b9d837033b700ce2 to your computer and use it in GitHub Desktop.
Dependent Values
This file contains hidden or 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 optionStuff | |
import scalaz._ | |
import Scalaz._ | |
object optionStuff extends App { | |
case class Test(a: Option[String], b: Option[String]) | |
case object Error | |
implicit val errorInstance = new Monoid[Error.type] { | |
def append(f1: Error.type, f2: => Error.type): Error.type = Error | |
def zero: Error.type = Error | |
} | |
type S[A] = Error.type \/ Option[A] | |
def createTest(a: S[String], b: S[String]): S[Test] = { | |
val x = (a |@| b).tupled | |
if (x.isLeft) Error.left else { | |
x.flatMap { | |
case (Some(x), y) => Test(x.some, y).some.right | |
case (None, None) => Test(none, none).some.right | |
case (None, Some(y)) => Error.left | |
case _ => none.right | |
} | |
} | |
} | |
assert(createTest("ha".some.right, "ba".some.right) == \/-(Some(Test(Some("ha"),Some("ba"))))) | |
assert(createTest("ha".some.right, none.right) == \/-(Some(Test(Some("ha"),None)))) | |
assert(createTest(none.right, "ba".some.right) == -\/(Error)) | |
assert(createTest(none.right, none.right) == \/-(Some(Test(None,None)))) | |
assert(createTest(Error.left, "ba".some.right) == -\/(Error)) | |
println("done") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment