I want to get the product of a List[Option[Int]]
, and flatten it to Option[Int]
:
List(Some(2), Some(3), Some(5)).foldLeft(Some(1)) { (x, y) =>
// err
x.map(ix => y.map(iy => ix * iy)).flatten
// // workaround
// x.map(ix => y.map(iy => ix * iy)).flatten match {
// case Some(acc) => Some(acc)
// case _ => Some(0)
// }
}
the err in detail:
polymorphic expression cannot be instantiated to expected type;
[error] found : [B]Option[B]
[error] required: Some[Int]
[error] x.map(ix => y.map(iy => ix * iy)).flatten
[error]
flatten
in foldLeft
didn't return Some[Int]
? What's going on?