3 ms·
But even that isn't weird, since parseInt() takes two arguments (string and radix) and map expects a function value that receives three arguments (value, index,
by open-paren 3y ago
But even that isn't weird, since parseInt() takes two arguments (string and radix) and map expects a function value that receives three arguments (value, index, and the whole array). It's not so much "weird" as it is "I didn't read the docs“.
- xigoi 3y agoIt's weird because someone coming from a functional language like Haskell would think that the anonymous function in the following code is redundant, while in fact it's necessary: ["10","10","10","10","10"].map(x => parseInt(x))
- zarzavat 3y agoIt’s nothing to do with the language being functional or not. It’s because JS supports optional arguments. Haskell doesn’t support optional arguments which is why this is impossible in Haskell, but that is a huge trade off since optional arguments make it easy to evolve an API over time without causing breakage. It’s a known good practice in JS to always wrap callbacks with a lambda in this situation. Even if you don’t, usually the type checking will catch it, this bug only works because the second argument is a number to both functions.
- tome 3y ago> optional arguments make it easy to evolve an API over time without causing breakage Defining functions with new names also make it easy to evolve an API over time without causing breakage.
- zarzavat 3y agoIt does tend to make code less readable however if you end up with several functions that do the same thing except that they were defined at different times with different numbers of arguments. It’s not always clear from the perspective of the API user whether the difference is just the number of arguments or if there are other differences too. Optional arguments communicate that intention more explicitly. Looking at, e.g. the Python standard library a large number of functions have arguments that were added after the function was first defined. If Python didn’t have optional/keyword arguments then the number of API calls would be substantially larger.
- tome 3y ago> It’s not always clear from the perspective of the API user whether the difference is just the number of arguments or if there are other differences too. Optional arguments communicate that intention more explicitly. They do, but potentially falsely. > Looking at, e.g. the Python standard library a large number of functions have arguments that were added after the function was first defined. If Python didn’t have optional/keyword arguments then the number of API calls would be substantially larger. The inability of Haskell not to have optional arguments isn't too bad, but all in all, I have to concede that you have a fair point. I personally don't miss optional arguments but I can see how they're beneficial (and I would really like named arguments).
- paulddraper 3y agoIt's weird for multiple reasons unique to JavaScript: 1. Functions can be called with the wrong number of arguments. 2. map() passes multiple arguments to the callback.