Re:FP techniques that will help you write better JavaScript
by dragonwriter on Friday February 11, 2022 @02:47PM
(#30306165)
> 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.)
Re:FP techniques that will help you write better JavaScript
by fulafel on Friday February 11, 2022 @03:21PM
(#30306531)
Yes, 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).
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.