Skip to content

Instantly share code, notes, and snippets.

@folone
Created June 30, 2011 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save folone/1055799 to your computer and use it in GitHub Desktop.
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
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