---
# System prepended metadata

title: Untitled

---


Implement a minimal `Option` that has `map` and `flatMap`.


```scala
import scala.{Option => _}
// implement here

sealed trait Option[A+] {
    //implement map and flatMap
    def map[B](f: A => B): Option[B] = {
        this match {
            case None => None
            case Some(a) => Some(f(a))
        }
    }
    
    def map2[B](f: A => B): Option[B] = {
        fold(None)(a: A => Option(f(a)))
    }
    
    def flatMap[B](f: A => Option[B]): Option[B] = {
        this match {
            case None => None
            case Some(a) => f(a)
        }
    }
    
    def flatMap2[B](f: A => Option[B]): Option[B] = {
        fold(None)(f)
    }
    
    
    def fold[B](b: B)(f: A => B): B = {
        this match {
            case None => b
            case Some(a) => f(a)
        }
    }
    
    
}


case object None extends Option[Nothing]
case class Some[A+](a: A) extends Option[A]

val a: Option[Int] = Some(3)

val foo: String = a.fold("Empty")(i => s"Some int [$i]")


```
