Created
December 16, 2012 22:42
-
-
Save anonymous/4313804 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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