DEV Community

Point-free gotchas in JavaScript

Dan Homola on February 26, 2018

Note: this post was originally published on my Medium profile I am a big fan of functional programming, I enjoy the conciseness of it and it fits ...
Collapse
 
seymen profile image
Ozan Seymen

Thanks for the article Dan!

I believe the solution to your first gotcha is "unary" (e.g. ramdajs.com/docs/#unary) - something like:

const R = require('ramda')

const nums = ["25", "45", "11"]
console.log(nums.map(R.unary(Number.parseInt)))
// logs [ 25, 45, 11 ]

I am a little confused with how your second gotcha (this losing context) is related specifically to the pointfree style. I thought this is a general issue with this and not specifically tied to pointfree. Your first example in the second gotcha also suggests that (no pointfree there). Am I missing something?

Collapse
 
danhomola profile image
Dan Homola

Thanks for the comment! The unary function seems neat, I will definitely give Ramda a closer look :)

As for the second gotcha it is simply to illustrate that () => foo() cannot always be replaced by foo as a more "point-free" style. I hope it is clearer now.

Collapse
 
tclift profile image
Tom Clift

In your bind example, it might be worth mentioning a related gotcha: bind sends arguments as additional parameters. In your getMessage example, if getMessage took a number, and you bound from a function with an unrelated number param, you get a nasty surprise!

Collapse
 
danhomola profile image
Dan Homola

Thanks for the addition, I did not realise that!

Collapse
 
baukereg profile image
Bauke Regnerus

Thanks for this well written article. It describes exactly the reason why I prefer closures over point-free functions. The short notation of point-free looks great, but the lack of control over the scope as well as the arguments passed in has caused many flaws in my Javascript code over the years.

Collapse
 
danhomola profile image
Dan Homola

Thank you for your kind comment, I'm glad you liked it :)

Collapse
 
coatesap profile image
Andy Coates

'Fix B' from above could be expressed slightly more elegantly as:

// Fix B - binding
const isActivationCodePointFree = RegExp.prototype.test.bind(/^\d{4}-\d{4}-\d{4}$/);