Created
June 11, 2012 09:14
-
-
Save andypetrella/2909215 to your computer and use it in GitHub Desktop.
Monad Transformer for Promise and Validation
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
case class ValidationPromised[E, A](promised: Promise[Validation[E, A]]) { | |
def map[B](f: A => B): ValidationPromised[E, B] = | |
ValidationPromised(promised map { valid => | |
valid.fold( | |
fail => KO(fail), | |
suc => OK(f(suc)) | |
) | |
}) | |
def flatMap[B](f: A => ValidationPromised[E, B]): ValidationPromised[E, B] = | |
ValidationPromised(promised flatMap { valid => | |
valid.fold ( | |
bad => Promise.pure(KO(bad)), | |
good => f(good).promised | |
) | |
}) | |
} |
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
for { | |
ns <- getNode(s); //get source | |
nt <- getNode(t); //get target | |
l <- link(ns, nt); //link them | |
} yield l //yield the link |
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
for { | |
vs <- getNode(s); //get source | |
vt <- getNode(t); //get target | |
vl <- for { | |
ns <- vs; | |
nt <- vt | |
} yield link(ns, nt); //link them | |
} yield vl //yield validation of the link |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment