Guest User

Untitled

a guest
Jan 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. trait Option[A] {
  2. def flatMap[B](f: A => Option[B]): Option[B]
  3. def map[B](f: A => B): Option[B]
  4. }
  5.  
  6. case class None[A]() extends Option[A] {
  7. def flatMap[B](f: A => Option[B]): Option[B] = None[B]
  8. def map[B](f: A => B): Option[B] = None[B]
  9. }
  10.  
  11. // return the give parameter wrapped in option
  12. def unit[T](aType: T): Option[T] = Some(aType)
  13.  
  14. case class Some[A](a: A) extends Option[A] {
  15. def flatMap[B](f: A => Option[B]): Option[B] = f(a)
  16. def map[B](f: A => B): Option[B] = unit(f(a))
  17. }
Add Comment
Please, Sign In to add comment