DEV Community

Discussion on: JS Functional Concepts: Pipe and Compose

Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

Sure!
If you apply good practices and split the code in single-responsibility functions you'll end up chaining quite a few of them.

In OOP you'll do something like:

function replaceBy  (target, replacement) {
  return num.toString().split(target).join(replacement)
}
Enter fullscreen mode Exit fullscreen mode

Whereas in functional programming it will look something like:

const replaceBy = (target, replacement) =>
  pipe(toString, split, join)([target, replacement);
Enter fullscreen mode Exit fullscreen mode

Which is objectively better than

const replaceBy = (target, replacement) =>
  toString(split(join(target, replacement)));
Enter fullscreen mode Exit fullscreen mode

Specially as the chain grows and for readability: you can actually read them from left to right in comma separated names, plus having just one initial arg usually helps to avoid side-effects.

So it's not a niche concept but a generic one, a nice to have (and use).

That's a bit of a silly example but it may work just to showcase, also you can find another example in the post 🙂

Some comments have been hidden by the post's author - find out more