I have a map and I want to transform it, via yield, to a new collection, filtering based on the keys. I only want a subset of the map entries to be in the new collection.
scala> val the_map = Map(1->10, 2->41, 3->41, 27->614, 400->541, 5214 -> 2)
the_map: scala.collection.immutable.Map[Int,Int] = Map(1 -> 10, 2 -> 41, 27 -> 614, 3 -> 41, 400 -> 541)
scala> val transformed = for ((k, v) <- the_map) yield {if (k < 10) { v * 10 } else if (k > 100 && k < 400) { v * 5 }}
transformed: scala.collection.immutable.Iterable[AnyVal] = List(100, 410, (), 410, 2705, ())
So I basically want that, but without the ()s in there, and the type to be Iterable[Int]. What's the right approach here? I could filter to remove the ()s and then cast, but that seems wrong. I could Some all the values and put a None at the bottom, and then call flatten, but that seems excessive. Is there a clean way to go? I just want yield to ignore the ()'s that are returned when no if statement matches.