So You Want to be a Functional Programmer (Part 3)
Charles Scalfani
2.2K25

Charles, is a very good job what you are doing in this series. Thanks for your time writing it.

I'm not so much into Elm yet, and it was kinda weird to me to read the composition flowing from right to left <<, instead of left to right >>. It seems so much natural to me to write from left to right, like we do when using pipe in *nix systems (e.g cmd1 | cmd2 | cmd3). It makes me wondering if is it a common practice on Elm's world, or is just your personal preference.

By the way, recently i wrote a module for JS that mimics the pipe behaviour from *nix. Usually i use it to have a chain of functions and promises in a clean, and more modular way. But reading this post i saw that it could also help with function composition.

With your first example of mult5AfterAdd10, with add10, the composition would be:

var add10 = value => value + 10;
var mult5 = value => value * 5;
var mult5AfterAdd10 = value => pipe(value, add10, mult5);

In your second example, having add instead of add10, it would be like:

var add = (x,y) => x + y;
var mult5 = value => value * 5;
var mult5AfterAdd10 = value => pipe(value, v => add(10, v), mult5);

And with multiple parameters, to define also the amount to add and to mult, we would have:

var add = (x,y) => x + y; 
var mult = (x,y) => x * y;
var multAfterAdd = (value, nToAdd, nToMult) => pipe(value, v => add(nToAdd, v), v => mult(nToMult, v));

Because the way i see (now) is that having a Function that was composed by N others, is like the same as creating a Function that would call a pipe of N functions within it.

I would like to hear from you what do you think about this way of composing functions. For reference, the lib is here: https://github.com/DiegoZoracKy/pipe-functions