2 ms·
> Sorry if it’s unfamiliar, but the index argument is critical to many real use-cases. Only because .map() is an Array method, not a method of a generic iterat
by dragonwriter 5y ago
> Sorry if it’s unfamiliar, but the index argument is critical to many real use-cases.
Only because .map() is an Array method, not a method of a generic iteration protocol applicable to other iterables (including generators.)
In Python, while map is a built-in function instead of a method, and you’ll probably want a comprehension instead, if you want the index for a map (or in a comprehension), you just wrap call enumerate() and go from an iterable of items to an iterable of pairs of (item, index), so:
map(f, enumerate(collection))
In Ruby, if you want the index in a map, you just do:
collection.each_with_index.map {|item, index| … }