var mappableParseInt = function(str){
return parseInt(str, 10);
};
['10', '10', '10'].map(mappableParseInt);
I'd suspect this snippet is more a snipe at people who don't know JS very well and expect parseInt to be base-10 only. var mappableParseInt = function(str){
return parseInt(str, 10);
};
['10', '10', '10'].map(mappableParseInt);
I'd suspect this snippet is more a snipe at people who don't know JS very well and expect parseInt to be base-10 only.But it's WTF anyway. I have a function that takes either one or two arguments, I provide three, and everyone seems to be OK with that.
There's no way to know, even at run-time, whether a function is being called with too few or too many arguments, since that's equivalent to the halting problem. So the sensible alternative is just to default everything to undefined, and silently ignore extraneous arguments.
But yes, if JS was strict with how it handled argument definition lists and had support for indicating infinite arity, I'd agree, this would be a WTF, or at least strange. But I think it makes a lot of sense, all things considered.
Rather: remember three things that make up the majority of Array iterators' callback functions' signatures:
Element, Index, Array.
Shared by: .map, .every, .forEach, .filter, and probably some that I am forgetting. The exception I think is just .reduce[Right], which by definition requires its previous return value, so you have (retVal, elem, i, arr).
Quite literally, if you remember .map callback, you remember .every callback :)
Javascript deserves shtick for its truly bad parts (with, arguments, ...) and some missing parts, but .map and its friends aren't it.
I dunno, as someone without much experience with Javascript, it is a little odd that arrays return the index alongside the value by default.
>>> ['10', '10', '10'].map(x => parseInt(x, 10))
[10, 10, 10]The programmers are not the ones to blame on that.. this is really a bad contract between the language and the programmer
Its the equivalent of a function named "getStone()" to return you a " Paper{} " :)
It does.
> and entirely unreasonable to expect to be required to provide a base as anything except an optional argument
It is optional.
> and certainly it is unreasonable to expect that that second optional argument is treated as not optional in a composition operation
I don't understand what you're saying here. It's never treated as required, it's just map supplies a parameter in that position, so it get's used. That's how optional parameters work.
The wat (if there is one) is that map provides extra arguments.
> It does.
Not quite: in some browsers (IE), a string starting with '0' gets interpreted as octal. So parseInt('041') === 33 in IE.
Guess how I found out about that.
I think in this case it's not parseInt that's at fault, it's the fact that map optionally passes additional arguments.