scala / scala-library-next

backwards-binary-compatible Scala standard library additions
Apache License 2.0
69 stars 17 forks source link

Add mapTo to IterableOnceOps #57

Open BalmungSan opened 3 years ago

BalmungSan commented 3 years ago

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?

neontorrent commented 3 years ago

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

SethTisue commented 3 years ago

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.

NthPortal commented 3 years ago

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

OndrejSpanel commented 3 years ago

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

hides the intention

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