Various methods on Maps end up returning a Iterable collection over key value pairs.
Handling these in Scala can sometimes seem a bit verbose or opaque, with my understanding that the choices seemingly being either a pair match, or using ._1
and ._2
.
E.g. groupMapReduce(_._1)(_._2)(_ + _)
or
groupMapReduce{ case (k, v) => k}{ case (k, v) => v}(_ + _)
I was wondering whether it would be helpful to have “key
” and “value
” methods added to Tuple2 as aliases for _1 and _2.
E.g., so the code above could be written as:
groupMapReduce(_.key)(_.value)(_ + _)
In Scala 3, we wouldn’t need the case keyword so the first code example would be shorter. Of course, there is also the choice that these could be added as extension methods, but really my question is whether it would be beneficial for these to be added to the standard library.