---
# 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] = fold(None)(f andThen Some.apply) 
    def flatMap[B](f: A => Option[B]): Option[B] = fold(None)(f)
    def fold[B](defaultValue: => B)(f: A => B): B
}

case object None extends Option[Nothing] {
    
    def flatMap[B](f: Nothing => Option[B]): Option[B] = None

    def fold[B](defaultValue: => B)(f: Nothing => B): B = defaultValue

}

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

    
    def flatMap[B](f: A => Option[B]): Option[B] = f(value)
    
    def fold[B](defaultValue: => B)(f: A => B): B = f(value)


}

Some(1).fold(5 / 0)(i => i + 1) // 

val o1: Option[Int] = Some(1)


val oInt: Option[Int] = None // Option[Nothing]
val oString: Option[String] = None // Option[Nothing]
Nothing <: Int => Option[Nothing] <: Option[Int]
[...] <: String => Option[[...]] <: Option[String]

String <: Any
Int <: Any

sealed trait Animal
case object Cat extends Animal

val myAnimal: Option[Animal] = Some(Cat) // Option[Cat]
Cat <: Animal => Option[Cat] <: Option[Animal]


```



A
|
B

Option[+A] means
Option[A]
|
Option[B]
