Skip to content

Instantly share code, notes, and snippets.

Created December 16, 2012 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save anonymous/4313804 to your computer and use it in GitHub Desktop.
Save anonymous/4313804 to your computer and use it in GitHub Desktop.
import scalaz._, Scalaz._
sealed trait MyOption[+A]
case class MySome[A](value: A) extends MyOption[A]
case object MyNone extends MyOption[Nothing]
object MyOption {
implicit object MyOptionFunctor extends Functor[MyOption] {
def map[A, B](m: MyOption[A])(f: A => B) = m match {
case MySome(a) => MySome(f(a))
case MyNone => MyNone
}
}
}
object Main extends App {
val m: MyOption[String] = MySome("1234")
assert(m.map(_.length) == MySome(4))
def f[F[_]: Functor](m: F[String]) = m map (_.toInt)
assert(f(m) == MySome(1234))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment