scala-library-next icon indicating copy to clipboard operation
scala-library-next copied to clipboard

Add mapTo to IterableOnceOps

Open BalmungSan opened this issue 4 years ago • 4 comments

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?

BalmungSan avatar Dec 17 '20 17:12 BalmungSan

I believe the new way of avoiding intermediate collection in 2.13 is using view after we got rid of CanBuildFrom.

neontorrent avatar Jan 20 '21 04:01 neontorrent

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.

SethTisue avatar Jan 20 '21 06:01 SethTisue

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

NthPortal avatar Jan 22 '21 09:01 NthPortal

foo.map(x => x -> f(x)).toMap

hides the intention

The code above is clearer than a foo.mapTo would be for me.

OndrejSpanel avatar Jan 22 '21 09:01 OndrejSpanel