TL;DR, parseInt() takes the number base as a second argument, and map() passes three arguments (value, index, whole array). Using directly like this a function with map() is just incorrect, the correct way to do it is: ['1', '7', '11'].map(x => parseInt(x)) edit I'm getting downvoted, it doesn't matter much but I don't understand it when the most upvoted comment seems to say more or less the same?
Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
151–160 of 163 posts
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#152The extra args passed by map coupled with the optional extra args in standard methods is the cause of a lot of confusion, but this feels like a missed opportunity for demonstrating functional programming. In the end author suggests ['1', '7', '11'].map(numStr => parseInt(numStr)); I think you'd learn something much more useful with function radixParser(radix) { return numStr => parseInt(numStr, radix); } ['1', '7', '…
If you're gonna use arrow functions, why not go all the way! const radixParser = (radix) => (numStr) => parseInt(numStr, radis);
function foo() { ... }
is a clearer way of indicating "I'm defining a function named foo". I tend to use arrows only for anonymous methods.Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#153Earlier quoted context omitted.
Did you try testing that? The TypeScript compiler does't say a thing about that code: https://www.typescriptlang.org/play/index.html#src=alert(%5B... TSLint similarly reports nothing even with the tslint:all ruleset: https://palantir.github.io/tslint-playground/?saved=N4Igxg9g... https://palantir.github.io/tslint-playground/
Doesn't seem right, are all the strict compiler options enabled?
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#154This isn't strange or surprising. parseInt takes two arguments, the second one is the radix and map will call with three arguments, the value, the index and the whole array. You just have to know this and it might be different in other languages. [ ... ].map(x => ...) is the right way to do this.
It may be logical but: - I know all that, have been programming for 15 years, yet I still would have done the mistake. - Simple unit tests may very well not catch this bug the first time. - The language design allows your brain to ignore the index parameter because JS accepts superfluous parameters, which is a terrible decision. - map() is a mapping primitive. It's supposed to adapt a type to another type so that you…
So have you not used PHP, or are you counting it as not-modern? :P
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#155Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#156Earlier quoted context omitted.
That’s a mailing list email from 2016, and is a joke about bunch of silly non-standard names for punctuation (e.g. “bang”, “wack”, and “twiddle”). Obviously occasional people are going to pick up terminology from other communities, and the name “splat” has been gaining popularity recently (I had literally never heard that term before a few years ago, and have been writing Python code since 2002). I occasionally hear…
The most famous JS template engine of the early 2000 used to be literally called "mustache" because of it: https://mustache.github.io/
Mustache is a Ruby library from 2009. Here’s the first commit https://github.com/mustache/mustache/commit/6ee6bcf21d381554...
But more to the point, someone calling their template library “mustache” because a curly brace has a vaguely mustache-like shape doesn’t remotely imply that people regularly call curly braces “mustaches” or would have any idea what “mustache” meant in the context of someone pronouncing their computer code.
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#157Earlier quoted context omitted.
That's apologist talk pure and simple. The language, silently, does something that's almost certainly wrong. The language has enough information to provide you with a helpful warning or error message that you probably don't want to do this, but instead, it violates the principle of least surprise by just doing the wrong thing instead. The correct error is something along the lines of: let numbers = input.iter().map(p…
If Rust had ergonomic support to pass multiple arguments to a functions you would still not be protected from this bug. Even the type system wouldn't help you there because the index would be of type usize and so would be the radix argument in parseInt (although in reality this would probably be an enum or u8). But this is caused by the interaction of two bad APIs. Even if we have the restriction that it confirms to…
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#158Earlier quoted context omitted.
It may be logical but: - I know all that, have been programming for 15 years, yet I still would have done the mistake. - Simple unit tests may very well not catch this bug the first time. - The language design allows your brain to ignore the index parameter because JS accepts superfluous parameters, which is a terrible decision. - map() is a mapping primitive. It's supposed to adapt a type to another type so that you…
> Certainly the worst of all modern stack languages So have you not used PHP, or are you counting it as not-modern? :P
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#159Earlier quoted context omitted.
It may be logical but: - I know all that, have been programming for 15 years, yet I still would have done the mistake. - Simple unit tests may very well not catch this bug the first time. - The language design allows your brain to ignore the index parameter because JS accepts superfluous parameters, which is a terrible decision. - map() is a mapping primitive. It's supposed to adapt a type to another type so that you…
As a (mostly) outsider, I’ve never understood why JavaScript is so popular in web dev circles. There are so many awesome compile-to-JS languages these days (ClojureScript, PureScript, Elm, ReasonML, Scala.js, etc). What makes people want to use JavaScript instead?
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#160Earlier quoted context omitted.
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?
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(…