My data type is: List[(Option[Set[Long]], Option[Set[(Long, Long)]])]
(i.e. a List of a 2-tuple of Options, one is for a Set of Long, one is for a Set of a 2-tuple of Longs). E.g.:
val l = List((Option(Set(1L, 2L, 3L)), Option(Set((4L, 5L), (6L, 7L)))))
I wish to flatMap this List into a List[Set[Long]]
type, in a way that each of the first elements of the second Option, would be concatenated to a new Set with the first Option. So the desired result here is:
List(Set(1L, 2L, 3L, 4L), Set(1L, 2L, 3L, 6L))
This works fine with the following code:
l.flatMap {
case(setOpt1, setOpt2) =>
val set1 = setOpt1.getOrElse(Set[Long]());
val set2 = setOpt2.getOrElse(Set[(Long, Long)]());
set2.map(k => set1 ++ Set(k._1))
}
The plot thickens when the second Option is None
, and the first Option "exists", e.g.:
val l = List((Option(Set(1L, 2L, 3L)), None))
And assume I have no way of avoiding this. In this case I would like to get the first Set as is:
List(Set(1L, 2L, 3L))
However, using the same code as above the set2
value becomes an empty Set, and the map
means nothing in this case, and I would get an empty List:
res60: List[scala.collection.immutable.Set[Long]] = List()
So, how do I do this? Preferably elegantly :)