Re:FP techniques that will help you write better JavaScript
by dragonwriter on Friday February 11, 2022 @02:44PM
(#30306111)
> 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:
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:
In Ruby, if you want the index in a map, you just do: