Created
June 30, 2011 07:25
-
-
Save folone/1055799 to your computer and use it in GitHub Desktop.
Monad implementation attempt. Following http://stackoverflow.com/questions/1992532/monad-trait-in-scala
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
trait Monad[+M[_]] { | |
def unit[A](a: A): M[A] | |
def bind[A, B](m: M[A])(f: A => M[B]): M[B] | |
} | |
// probably only works in Scala 2.8 | |
implicit def monadicSyntax[M[_], A](m: M[A])(implicit tc: Monad[M]) = new { | |
private val bind = tc.bind(m) _ | |
def map[B](f: A => B) = bind(f compose tc.unit) | |
def flatMap[B](f: A => M[B]) = bind(f) | |
} | |
implicit object MonadicOption extends Monad[Option] { | |
def unit[A](a: A) = Some(a) | |
def bind[A, B](opt: Option[A])(f: A => Option[B]) = opt flatMap f | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment