For example,
Vector(Some(1), Some(2), Some(3), None).flatMap{
n => n
}
produces a Vector(1, 2, 3)
instead of giving an error. As I have seen in other languages, flatMap
is used when you have a mapper function that produces nesting so I would expect this to be a valid flatMap
:
Vector(1, 2, 3).flatMap{
eachNum => Vector(eachNum)
}
My mapper function produces a Vector
which would cause nesting (i.e. Vector(Vector(1), Vector(2), Vector(3), Vector(4))
) if I used a map
due to the container wrapping. However, flatMap
will remove this nesting and flatten it. This makes sense when there is nesting of two identical monads.
However, I do not understand how using a flatMap
with a mapper function that returns an Option
makes a Vector[Option[Int]]
become a Vector[Int]
. Is there some sort of transformation going on (I have never seen this before), could someone explain and perhaps point me to some resources?
Thank you very much
:replay -Xprint:typer
. – Alysa