News for hackers. Stuff that matters.
Re:The Birth and Death of JavaScript [video] by granttimmerman on Thursday April 17, 2014 @04:58PM (#7606289)
> xs = ['10', '10', '10']

> xs.map(parseInt)

[10, NaN, 2]

Javascript is beautiful.

Re:The Birth and Death of JavaScript [video] by just2n on Thursday April 17, 2014 @05:10PM (#7606386)
There are so many good WTFs in JS, but this is not one. parseInt expects 2 arguments and Array.prototype.map provides 3 to the callback it is given. Both of these facts are very well documented and known.

    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.
Re:The Birth and Death of JavaScript [video] by hcarvalhoalves on Thursday April 17, 2014 @08:55PM (#7607436)
I don't think `parseInt` accepting an optional second argument is the surprising behavior there. The real WTF is `map` passing more than one argument, and the loose behavior of JS regarding argument passing overall.
Re:The Birth and Death of JavaScript [video] by briantakita on Friday April 18, 2014 @03:38AM (#7608320)
It's only WTF because it's not the same as other implementations of map. Once you can internalize the map implementation, it's no longer WTF & actually makes sense.
Re:The Birth and Death of JavaScript [video] by meowface on Friday April 18, 2014 @06:36AM (#7608688)
It makes sense but it still strongly violates the principle of least surprise. No other language I know of does this, nor do I think this would be a particularly desirable feature.
Re:The Birth and Death of JavaScript [video] by rplnt on Friday April 18, 2014 @04:43AM (#7608482)
That's the weird part. Why does array provide three arguments? But I agree, that's something you can learn. I guess.

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.

Re:The Birth and Death of JavaScript [video] by just2n on Friday April 18, 2014 @05:10PM (#7611346)
Well that decision is pretty necessary when you realize that JS has no syntax to indicate a function is variadic (we use the arguments magic variable, but use of it does not necessarily indicate that a function is variadic) and that implementation supplied functions are not required to have their arity exposed via Function.prototype.length (http://es5.github.io/#x15.3.5.1).

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.

Re:The Birth and Death of JavaScript [video] by gnuvince on Friday April 18, 2014 @10:07AM (#7609166)
So much for abstraction if you need to understand the implementation of every function you'll every use in JavaScript.
Re:The Birth and Death of JavaScript [video] by yourad_io on Friday April 18, 2014 @10:39AM (#7609297)
> understand the implementation of every function

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.

Re:The Birth and Death of JavaScript [video] by CatMtKing on Thursday April 17, 2014 @05:45PM (#7606629)
You could say it's a snipe at the weak type system that Javascript has.

I dunno, as someone without much experience with Javascript, it is a little odd that arrays return the index alongside the value by default.

Re:The Birth and Death of JavaScript [video] by anaphor on Thursday April 17, 2014 @07:23PM (#7607159)
It has little to do with the type system. Variadic arguments can be typed given a type system that supports it.
Re:The Birth and Death of JavaScript [video] by xiaomai on Thursday April 17, 2014 @06:29PM (#7606907)
arrays don't do that, but map() does. Normally in js you can just ignore arguments you don't care about, but it does lead to surprises like this one.
Re:The Birth and Death of JavaScript [video] by clebio on Thursday April 17, 2014 @08:59PM (#7607446)
Alternatively, if a function expects two arguments, the language could take exception at the fact that three were handed in. Quietly accepting arbitrary arguments could be considered breaking contract. It does have a wtf-ey whiff.
Re:The Birth and Death of JavaScript [video] by ahoge on Friday April 18, 2014 @04:24AM (#7608442)
A bit less annoying with ES6:

  >>> ['10', '10', '10'].map(x => parseInt(x, 10))
  [10, 10, 10]
Re:The Birth and Death of JavaScript [video] by oscargrouch on Thursday April 17, 2014 @09:19PM (#7607506)
with a function named "parseInt" anyone would expect the inputs as base 10.. otherwise this shoud be called "parseHex" for 15 or at least "parseBytes(input, base)"

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{} " :)

Re:The Birth and Death of JavaScript [video] by djur on Thursday April 17, 2014 @09:31PM (#7607539)
"int" doesn't imply anything about the base.
Re:The Birth and Death of JavaScript [video] by Iftheshoefits on Thursday April 17, 2014 @10:50PM (#7607735)
It does for human beings. We use base-10 for basically everything. This is true even for most programmers in most situations. Human beings aren't computers, and we aren't abstract math processing units who by default consider numbers abstracted (e.g. as elements of Rings). This goes double for string representations of numbers--in the majority of cases a number represented by a string is a number meant for human consumption in a normal human context; not some machine running in base-2 (or -8 or -16). It is certainly reasonable to expect "parseInt" to parse an integer out of a string in base-10 by default, and entirely unreasonable to expect to be required to provide a base as anything except an optional argument, and certainly it is unreasonable to expect that that second optional argument is treated as not optional in a composition operation.
Re:The Birth and Death of JavaScript [video] by ajanuary on Friday April 18, 2014 @04:36AM (#7608463)
> It is certainly reasonable to expect "parseInt" to parse an integer out of a string in base-10 by default

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.

Re:The Birth and Death of JavaScript [video] by nollidge on Friday April 18, 2014 @10:50AM (#7609350)
>> ...parse an integer out of a string in base-10 by default

> 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.

Re:The Birth and Death of JavaScript [video] by djur on Friday April 18, 2014 @04:32PM (#7611155)
I agree with your conclusions. However, the poster I was responding to was suggesting that the category of "int" necessarily excludes non-decimal representations in the same sense that the category of "stone" excludes "paper".

I think in this case it's not parseInt that's at fault, it's the fact that map optionally passes additional arguments.

Re:The Birth and Death of JavaScript [video] by TazeTSchnitzel on Thursday April 17, 2014 @05:04PM (#7606333)
It's due to parseInt having an optional second parameter, the radix, and map passing the index as the second paramater, hence:

  xs = [
    parseInt('10', 0),
    parseInt('10', 1),
    parseInt('10', 2)
  ]
Re:The Birth and Death of JavaScript [video] by jisaacks on Thursday April 17, 2014 @05:09PM (#7606380)
And since 0 is falsy we get 10 for base 0.
Re:The Birth and Death of JavaScript [video] by kevincennis on Thursday April 17, 2014 @08:28PM (#7607354)
It's not exactly checking for falsy values. Although all falsy values will lead to a radix of 10 being applied.

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.

Re:The Birth and Death of JavaScript [video] by meowface on Friday April 18, 2014 @06:36AM (#7608691)
This is HN, there's no such thing as being pedantic. :)
Re:The Birth and Death of JavaScript [video] by mturmon on Saturday April 19, 2014 @01:01AM (#7612638)
Although keep in mind that excessive pedantry is frowned upon.
Re:The Birth and Death of JavaScript [video] by Thirdegree on Saturday April 19, 2014 @12:55AM (#7612627)
Or rather, there is but it's thoroughly welcome.
Re:The Birth and Death of JavaScript [video] by TazeTSchnitzel on Thursday April 17, 2014 @05:12PM (#7606401)
Which is odd. I wonder why they checked for falsiness and not the argument being undefined.
Re:The Birth and Death of JavaScript [video] by davidkassa on Friday April 18, 2014 @10:46AM (#7609329)
Thank you. I read many comments to find an explanation.
Re:The Birth and Death of JavaScript [video] by octatone2 on Friday April 18, 2014 @02:24AM (#7608159)
It's not optional if you lint your code :)
Re:The Birth and Death of JavaScript [video] by conradk on Thursday April 17, 2014 @05:19PM (#7606457)
Just like with any language, as long as you read the docs of the stuff you use, you don't get this problem (you might get others with automatic type conversion and missing arguments like the speaker says, but not this)... this is just stupid.

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.

Re:The Birth and Death of JavaScript [video] by coldtea on Thursday April 17, 2014 @06:29PM (#7606903)
The thing is, good language design means you don't have to read the docs.

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).

Re:The Birth and Death of JavaScript [video] by elwell on Thursday April 17, 2014 @09:41PM (#7607570)
> good language design means you don't have to read the docs

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.

Re:The Birth and Death of JavaScript [video] by coldtea on Thursday April 17, 2014 @10:15PM (#7607652)
That's because:

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.

Re:The Birth and Death of JavaScript [video] by 1stop on Thursday April 17, 2014 @09:53PM (#7607596)
But the user of javascript will have more bugs.
Re:The Birth and Death of JavaScript [video] by nathansobo on Thursday April 17, 2014 @06:45PM (#7606994)
Both behaviors make sense in isolation. It's not always so easy.
Re:The Birth and Death of JavaScript [video] by gary_bernhardt on Thursday April 17, 2014 @08:01PM (#7607282)
Juggling these interactions is exactly what makes good language design so difficult and time consuming. In the talk, I mention JS' ten-day design time several times for exactly this reason. Language design is hard and ten days just isn't enough time to carefully consider how everything will fit together. Try to imagine a programmer, even a brilliant one, noticing the map/parseInt interaction ten days after starting to learn JS, especially in 1995 when these high level languages were far less common. Seems unlikely!
Re:The Birth and Death of JavaScript [video] by tolmasky on Thursday April 17, 2014 @09:04PM (#7607465)
Arguably the issue is not with the behavior but rather a deeper design problem within the language itself. Notice that in Obj-C no one would ever get confused regarding the second parameter of parseInt:withRadix:.
Re:The Birth and Death of JavaScript [video] by DonHopkins on Thursday April 17, 2014 @07:59PM (#7607275)
By "this problem" do you mean "the this problem"?

And by "this is just stupid" do you mean "this === just stupid"?

If you think that's bad, you should see this!

Re:The Birth and Death of JavaScript [video] by nathansobo on Thursday April 17, 2014 @06:41PM (#7606974)
I just wrote this line about two hours ago and my tests weren't thorough enough to catch the bug it introduced. Just when I thought I knew JavaScript. Thanks for saving me some time.
Re:The Birth and Death of JavaScript [video] by bsder on Thursday April 17, 2014 @07:09PM (#7607096)
Always give the base to parseInt in Javascript.

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.

Re:The Birth and Death of JavaScript [video] by ajanuary on Friday April 18, 2014 @04:43AM (#7608480)
Not just javascript, a whole bunch of language's parseInt implementation will interpret the base from a leading zero etc.
Re:The Birth and Death of JavaScript [video] by TazeTSchnitzel on Thursday April 17, 2014 @08:41PM (#7607391)
Are there any other problems aside from octal?
Re:The Birth and Death of JavaScript [video] by iamthepieman on Thursday April 17, 2014 @06:46PM (#7606998)
I'm sure you know this by now but you can keep the syntax and use Number instead

xs.parseInt(Number)

Re:The Birth and Death of JavaScript [video] by matb33 on Thursday April 17, 2014 @07:26PM (#7607164)
I think xs.map(Math.floor) takes the cake if I recall speed tests properly
Re:The Birth and Death of JavaScript [video] by _random_ on Saturday April 19, 2014 @02:26PM (#7614212)
Always remember - it's not a language but a loosely parsable texty thing.
Re:The Birth and Death of JavaScript [video] by ajanuary on Friday April 18, 2014 @04:49AM (#7608494)
A useful function

    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.
Re:The Birth and Death of JavaScript [video] by nollidge on Friday April 18, 2014 @10:54AM (#7609379)
I think you accidentally a word...

> as an indicator of base.

As an indicator of base 8 (octal).

Re:The Birth and Death of JavaScript [video] by v413 on Thursday April 17, 2014 @07:46PM (#7607239)
You need:

['10', '10', '10'].map(Number);

Re:The Birth and Death of JavaScript [video] by octatone2 on Friday April 18, 2014 @02:30AM (#7608171)

  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.
Re:The Birth and Death of JavaScript [video] by medikoo on Friday April 18, 2014 @06:53AM (#7608718)
Where people use parseInt, they usually should use Number: ['10', '10', '10'].map(Number); // [10, 10, 10]

;)