DEV Community

Discussion on: Functional compose

Collapse
 
th0rgall profile image
Thor Galle • Edited

Nice article series!

Just pointing out: an alternative that could give even cleaner code for this specific calculation example is to use higher-order functions.

So instead of:

const number = 5;
const add = (base, adder) => base + adder;
const multiply = (base, multiplier) => base * multiplier;

const result = composeWith(
  number,
  product => add(product, 1), 
  number => multiply(number, 2) 
);

You could have this:

const number = 5;
const add = adder => base => base + adder;
const multiply = multiplier => base => base * multiplier;

const result = composeWith(
  number,
  add(1),
  multiply(2)
);

This way you're creating the functions on the fly in composeWith. I'd argue that comes closer to natural language ("first multiply by 2, then add 1").

This is basically a teaser for your next article about currying :)

Collapse
 
jamesrweb profile image
James Robb

Yeah, I had considered that when writing the article but I was wondering if beginner devs might get confused somewhat with executing a function in the queue before the queue is run.

Seeing your example though, I may have to reconsider that since it isnโ€™t as it was in my head at first glance and you have a point that it feeds into the next article on currying anyway.

By the way, I noticed you work at Columbia road, Iโ€™m at Futurice so we are basically working at sister companies ๐Ÿ˜…... small world!

Collapse
 
th0rgall profile image
Thor Galle • Edited

Yeah, I see that higher-order functions like that could be confusing if used without much explanation.

But your currying examples make these higher-order functions even nicer to create (eg. curriedAdd). It could be cool to refer back to this example at the end of the currying article, as a kind of "application of what we have learned: now we can make the composition look cleaner!)

By the way, I noticed you work at Columbia road, Iโ€™m at Futurice so we are basically working at sister companies ๐Ÿ˜…... small world!

This is no coincidence ;) I came here through the last Futu Dev Breakfast newsletter that linked to your article about piping!

Thread Thread
 
jamesrweb profile image
James Robb

I didn't even know Lucia added my article there... Fair enough though, good to know!