I have two questions but I expect the answers are intertwined. So I was playing around with flatMapping different parametric types. I get the following:
val s: List[String] = List("f2", "df", "e") //> s : List[String] = List(f2, df, e)
val o = s.map(s => if (s.head == 'f')Some(s) else None)
//> o : List[Option[String]] = List(Some(f2), None, None)
val o1 = s.flatMap(s => if (s.head == 'f')Some(s) else None)
//> o1 : List[String] = List(f2)
val a: Option[String] = Some("Hello") //> a : Option[String] = Some(Hello)
val a1 = a.map(s => s.toList) //> a1 : Option[List[Char]] = Some(List(H, e, l, l, o))
but
val a2 = a.flatMap(s => s.toList) //gives
//type mismatch; found : List[Char] required: Option[?]
So I'm trying to understand the logic behind o1 compiling but not a2. And then looking into Option I was wondering why Option doesn't inherit from the traits: Seq and Set? Option is a Seq as it maintains order, and it is a Set because it contains no duplicates. Through Seq and Set it would inherit from Iterable and Traversable.