Created
August 10, 2014 19:28
-
-
Save stew/310b7534481e3ceefe1d to your computer and use it in GitHub Desktop.
This file contains 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
trait MyOption[+A] { | |
// single abstract method | |
def cata[X](some: A => X, none: => X): X | |
def map[B](f: A => B): MyOption[B] = error("todo") | |
def flatMap[B](f: A => MyOption[B]): MyOption[B] = error("todo") | |
def getOrElse[AA >: A](e: => AA): AA = error("todo") | |
def filter(p: A => Boolean): MyOption[A] = error("todo") | |
def foreach(f: A => Unit): Unit = error("todo") | |
def isDefined: Boolean = error("todo") | |
def isEmpty: Boolean = error("todo") | |
// WARNING: not defined for None | |
def get: A = error("todo") | |
def orElse[AA >: A](o: MyOption[AA]): MyOption[AA] = error("todo") | |
def toLeft[X](right: => X): Either[A, X] = error("todo") | |
def toRight[X](left: => X): Either[X, A] = error("todo") | |
def toList: List[A] = error("todo") | |
def iterator: Iterator[A] = error("todo") | |
} | |
object MyOption { | |
def none[A] = new MyOption[A] { | |
def cata[X](s: A => X, n: => X) = n | |
} | |
def some[A](a: A) = new MyOption[A] { | |
def cata[X](s: A => X, n: => X) = s(a) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment