3 ms·
Sorry if it’s unfamiliar, but the index argument is critical to many real use-cases. As far as ignoring errors silently, sure, but I’m not even sure what the e
by brundolf 5y ago
Sorry if it’s unfamiliar, but the index argument is critical to many real use-cases.
As far as ignoring errors silently, sure, but I’m not even sure what the error would be in this case. Index is a number and radix is a number; even typescript won’t complain about this because the types line up.
- fulafel 5y agoIs it critical in some way that's absent from all the other langugaes that have a map()? There are standard, composable, explicit patterns in Python, Clojure, and I presume other map()-equipped anguages to get you the indices along with the items. (enumerate in Python and map-indexed in Clojure) The error I was talking about was the argument count mismatch erorr, that would have taught you on all the other times you used map() without knowing about the extra index argument.
- dragonwriter 5y ago> Is it critical in some way that’s absent from all the other langugaes that have a map()? Yes, but only because of other non-ergonomic design decisions of the API its part of (particuarly, that it is an eager API attached to the Array class returning Arrays; if it were a lazy API of generalized iterable protocol that returned and could be used on generators, then you could just also have an “enumerate” and “with_collection” companion methods, but as it is, even if you made standalone functions for those purposes, for the output to be used with .map() you need them to return Arrays, which means you are accumulating bulky intermediate results.)
- fulafel 5y agoYes, eagerness would use additional space in the case of a Python enumerate() style solution. This would be acceptable in my view and aligned with the general JS design. But an alternative is the map-indexed style solution (aka the current Array.map with a different name).
- 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| … }
- deleted 5y ago[deleted]
- fulafel 5y agoI ran across this anecdote about how people don't know (or acknowledge?) the existence of the index argument of Array.map. This is a link to the manuscript of the well regarded book on JS FP techniques "Javascript Allonge". The book was linked elsewhere in these comments. The index arg is not mentioned! https://github.com/raganwald/javascript-allonge/blob/db7c435d8b74a6177d0fc6b4f09baa9a3112283b/manuscript/markdown/References%20and%20Rebinding/recipes/map-with.md https://github.com/raganwald/javascript-allonge/blob/db7c435...