---
# System prepended metadata

title: Untitled

---

Implement a minimal `Option` that has `map` and `flatMap`.
```scala
import scala.{Option => _}
// implement here


sealed trait Option[+A] {
    
    def map[B](f : A => B) : Option[B] = {
        this match {
            case None => None
            case Some(value) => Some(f(value))
        }
        
        
        fold(None, Some.apply compose f)
        fold(None, f andThen Some.apply)

        fold(None, a => Some(f(a)))
    }
    
    def flatMap[B](f : A => Option[B]) : Option[B] = {
        this match {
            case None => None
            case Some(value) => f(value)
        }
        
        
        fold(None, f)
    }
    
    def fold[Res](notDef : => Res, defined : A => Res) : Res = {
        this match {
            case None => notDef
            case Some(value) => defined(value)
        }
    }
    
    
}

case object None extends Option[Nothing]

case class Some[A](value : A) extends Option[A]

Some(10).fold(5 / 0, _ + 1)


// None <: Some(anything)

val x = None
val y = Some(10)

val noneInt: Option[Int] = None
val noneString: Option[String] = None

Nothing <: T
=> Option[Nothing] <: Option[T]