Search code examples
javascriptpointfree

Why doesn't mapping over `parseInt` point-free style work as expected?


I'm running the following in the Node.JS console:

> ["30", "31", "32"].map(x => parseInt(x))
[ 30, 31, 32 ]
> ["30", "31", "32"].map(parseInt)
[ 30, NaN, NaN ]

Why aren't these expressions identical? Is there a semantic difference in calling a function in point-free style as opposed to an anynomous function?


Solution

  • ["30", "31", "32"].map(function(){
      console.log(arguments);
    });

    The issue is most likely related to map passing more than just the element to the callback. It passes the element, index, and the whole array. So if you are letting the map pass all that into the parseInt method, the index is most likely causing issues with the method knowing how to convert the string to a number of the related base.