Let's consider the following code:
Set(1, 2, 3, 4, 5)
.map(k => (k, if (k % 2 == 0) "even" else "odd"))
.toMap
Is there a way to simplify this in Scala, avoiding the creation of the intermediate set? I'll be doing something performance-sensitive on big collections and it wouldn't hurt to get the Map
created on-the-spot instead.
If nothing better comes along, I was thinking of ending up implementing something like this:
Map.from(S, k => if (k % 2 == 0) "even" else "odd"))
I'm still using Scala 2.12.
Thanks