scala-library-next
scala-library-next copied to clipboard
Add mapTo to IterableOnceOps
I have found myself wanting to map a collection into a Map similar to groupMap but where the key is always the identity and the groups will always have one element.
It is worth to say that you can represent that as foo.map(x => x -> f(x)).toMap and maybe dropping an iterator before the map to avoid the intermediate collection. But, IMHO, it is too much boilerplate and hides the intention.
Thus, I propose the addition of a mapTo or mapWith (being honest the naming is the worst part) method to any IterableOnce.
What do you all think?
I believe the new way of avoiding intermediate collection in 2.13 is using view after we got rid of CanBuildFrom.
The need does sometimes come up, but my sense is that it doesn't come up that often. If a method existed, I suspect people wouldn't remember what it does without looking it up, if that's some kind of metric for whether something is worth adding.
We have a ton of collections methods already, the bar is pretty high for adding more.
I added a zipMap extension method at work at some point, though it's just equivalent to .map(x => x -> f(x)). I feel like it's perhaps worth discussing adding this or something like it to collection-contrib, I think I agree with Seth that the use case isn't compelling enough for it to be in the standard library
foo.map(x => x -> f(x)).toMap
hides the intention
The code above is clearer than a foo.mapTo would be for me.