scala / scala-library-next

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

Add mapWithIndex #78

Open BalmungSan opened 3 years ago

BalmungSan commented 3 years ago

I found myself doing this a lot:

someCollection.iterator.zipWithIndex.map { case (a, idx) => f(a, idx) }.to(SomeCollectionType)

Which is a little bit verbose just to avoid allocating the intermediate collection. Additionally to the semantic improvement, since map is just a simple loop it is very easy to keep track of the index without any kind of overhead.