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.
I found myself doing this a lot:
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.