Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Forked from anonymous/functor.scala
Created December 16, 2012 22:46
Show Gist options
  • Save j5ik2o/4313826 to your computer and use it in GitHub Desktop.
Save j5ik2o/4313826 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