Skip to content

Instantly share code, notes, and snippets.

@andypetrella
Created June 11, 2012 09:14
Show Gist options
  • Save andypetrella/2909215 to your computer and use it in GitHub Desktop.
Save andypetrella/2909215 to your computer and use it in GitHub Desktop.
Monad Transformer for Promise and Validation
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
)
})
}
for {
ns <- getNode(s); //get source
nt <- getNode(t); //get target
l <- link(ns, nt); //link them
} yield l //yield the link
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