Skip to content

Instantly share code, notes, and snippets.

@h-hirai
Created November 17, 2012 06:29
Show Gist options
  • Select an option

  • Save h-hirai/4093810 to your computer and use it in GitHub Desktop.

Select an option

Save h-hirai/4093810 to your computer and use it in GitHub Desktop.
wakame monad maybe http://connpass.com/event/1152/
abstract class MyMaybe[T] {
def flatMap[U](f:T=>MyMaybe[U]):MyMaybe[U]
def map[U](f:T=>U):MyMaybe[U]
}
case class MyNothing[T]() extends MyMaybe[T] {
def flatMap[U](f:T=>MyMaybe[U]):MyMaybe[U] = MyNothing()
def map[U](f:T=>U):MyMaybe[U] = MyNothing()
}
case class MyJust[T](elm:T) extends MyMaybe[T] {
def flatMap[U](f:T=>MyMaybe[U]):MyMaybe[U] = f(elm)
def map[U](f:T=>U):MyMaybe[U] = MyJust(f(elm))
}
val db = Map("x"->12, "y"->3, "z"->20)
def myget(db:Map[String,Int], k:String):MyMaybe[Int] = db.get(k) match {
case Some(v) => MyJust(v)
case None => MyNothing()
}
val c = for (a <- myget(db, "x");
b <- myget(db, "y")) yield (a + b)
val f = for (d <- myget(db, "z");
e <- myget(db, "w")) yield (d + e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment