DEV Community

Cover image for Piping JavaScript

Piping JavaScript

K on February 04, 2018

Cover image by arbyreed on Flickr. JavaScript is getting more and more functional programming features, an exiting one is the new pipeline operato...
Collapse
Β 
brzdev profile image
π”žπ”žπ”―π”¬π”« πŸ…₯ β€’

As an avid Elixir user this couldn’t make me happier!

Collapse
Β 
codebryo profile image
Roman Kuba β€’

As I didn't come around to try this yet, I have one question.
Does the pipe operator know where to put the output of the previous function as input on the next one?
I am asking because:

const add2Days = addDays(2);

Takes the value as the second param.

const customFormat = format("D MMMM YYYY");

though takes the value as the first param.

Overall I think it's awesome that this comes to JS.
And a thank you for the posts you are putting out. Always looking forward to them.

Collapse
Β 
kayis profile image
K β€’ β€’ Edited

I think I wrote it wrong. It always uses the first argument. fixed it.

Collapse
Β 
alexcasalboni profile image
Alex Casalboni β€’

Coming from the "old world", it looks a bit weird at first :)

Looking at |> addDays(2), I would expect addDays(2) to be evaluated before being used as a function. In my mind, this can only work if addDays(2) actually returns a function.

Collapse
Β 
kayis profile image
K β€’ β€’ Edited

> In my mind, this can only work if addDays(2) actually returns a function.

This is what it does :)

A regular version of that function would look like that:

function addDays(amount, date) {...}

addDays(2, date);

A curried version looks like that:

const addDays = amount => date => {...};

addDays(2)(date);
Collapse
Β 
functionalstoic profile image
JasonSooter β€’ β€’ Edited

I enjoy the pipe function in RamdaJS. It's gonna be nice to have this natively.

Collapse
Β 
skyrpex profile image
Cristian PallarΓ©s β€’

I guess it's the same as the flow function in Lodash, isn't it? I always use it!

Collapse
Β 
kayis profile image
K β€’

RamdaJS is pretty awesome, yes.

Collapse
Β 
vekzdran profile image
Vedran Mandić ‒

I love it, and truly believe this is one small (big) step to popularize FP. Can be retrofitted with a pipe() fn (hint RamdaJS) for those who dislike the operator (for now!). Cool summary.

Collapse
Β 
Sloan, the sloth mascot
Comment deleted
Collapse
Β 
functionalstoic profile image
JasonSooter β€’

Why? It's a common and very much enjoyed operator in multiple other FP languages.

Collapse
Β 
guid75 profile image
Guid75 β€’

Do you really prefer function1(function2(function3(function4(param))))?

Collapse
Β 
kayis profile image
K β€’

Could you elaborate?

Collapse
Β 
rapasoft profile image
Pavol Rajzak β€’

So, is this only syntactic sugar? I personally don't see any increase in readability when I compare |> something() to .something().

Collapse
Β 
kayis profile image
K β€’ β€’ Edited

Yes.

You could see it as the FP equivalent to the dot operator of OOP.

v |> f to o.m()