Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in...
by pennaMan on Friday June 21, 2019 @11:24AM
(#20243316)
The map function fully expects a function with two arguments, as per documentation. Why would passing a function that takes two arguments to map be an error?
[ Parent
| on HN ]
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in...
by estebank on Friday June 21, 2019 @02:22PM
(#20245073)
The map function has a bad API surface that interacts poorly with the way people expect it to work. In a better designed API, if you needed the index of each yielded value other languages have some kind of enumerate method that will allow people opt-in to getting the index. Then if you needed the replicate the presented situation you would have something like
['1', '7', '11'].enumerate().map(([val, idx]) => parseInt(val, idx))
I understand that this pattern of having extra "helper" arguments is pervasive in JS and might seem less exotic to the community, but I feel it is a problematic approach, as it is foot-gun prone.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in...
by benschulz on Friday June 21, 2019 @11:36AM
(#20243418)
The example given is taken from Rust. Rust does not allow overloading (even in terms of optional parameters, I believe). Well, Rust allows overloading by implementing different traits but then forces you to disambiguate where necessary.