by granttimmerman · 12y ago · 0 comments
> xs.map(parseInt)
[10, NaN, 2]
Javascript is beautiful.
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.
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{} " :)
xs = [
parseInt('10', 0),
parseInt('10', 1),
parseInt('10', 2)
]parseInt internally uses the ToInt32 abstract operation on the radix parameter. Once it has that value, it explicitly looks to see if the value is 0. If it is, it uses a radix of 10.
https://people.mozilla.org/~jorendorff/es6-draft.html#sec-pa...
Edit: I hope that doesn't come off as pedantic. My point wasn't to disagree as much as it was to just add some further explanation.
Try this:
int subtract(int b, int a) { return a - b; }
int test = subtract(5, 3); // != 2, just read the damn docs
Oh, C sucks now !
The talk is quite fun and interesting to watch though.
And the end is pretty cool.
The number one thing taught at user interaction / usabillity courses is that users don't read the documentation. Or skim it and go directly to one or two parts they want to check (Sure, some bizarro outliers do read it all).
Besides, a golden rule from the UNIX era is the "principle of least surprise". Don't define stupid behavior as default, as in this case (both for parseInt and Map).
Let's assume JavaScript is a poorly-designed language, and Clojure is a well-designed language. In the first month of language use, the user of Clojure will have looked at the docs many more times.
1) Clojure has a larger API -- Javascript doesn't have 1/10 that.
2) Javascript has a familiar (to many) Algol-derrived braced syntax and lots of common C/C++/Java/etc keywords. Clojure is only familiar to Lisp/Scheme users.
If those things were equal, Clojure would win the "don't have to look caveats up" contest, because its design is more coherent, and doesn't give you unexpected results and undefined behavior like Javascript does.
Obviously, you somehow you need to first know that "parseInt" is called "parseInt()" and not "atoi()" for example. But I wasn't implying never reading anything, including function reference. Just being able to code without needing to study and/or memorize lots of arcane edge cases.
And by "this is just stupid" do you mean "this === just stupid"?
If you think that's bad, you should see this!
Always. The moment you don't, all kinds of bugs follow.
I have to go cry now at the number of times this has bitten me.
xs.parseInt(Number)
function overValues(f) { return function(x) { return f(x); } }
Then you can do ['10', '10', '10'].map(overValues(parseInt));
However, usually you're going to want to do the equivalent of this ['0101', '032'].map(function(s) { return parseInt(s, 10); })
Because Javascript interprets a leading zero as an indicator of base.> as an indicator of base.
As an indicator of base 8 (octal).
['10', '10', '10'].map(Number);
var xs = ['10', '10', '10'];
xs.map(function (str) {
return parseInt(str, 10);
});
> [10, 10, 10]
Fixed that for you. Why? map callback params: (value, index, originalArray)
parseInt params: (string, radix)
Your code is passing the map array index to parseInt's radix.;)