Luke Warren Dev
Software Development Tips and Stories When I feel Like It

When Pipe Isn't Long Enough

Meet the long pipe.

,
Categories: ramda , fp
Tags: ramda , pipe

Pipe, a popular function in ramda, only allows 6 functions to be used in a pipeline. This limitation is usually fine but there are rare usecases where you might need more than 6. Meet the long pipe.

const { curry, head, tail } = require("ramda");

const longPipe = curry((fns, value) => {
  if (fns && fns.length > 0) {
    const nextFn = head(fns);
    const newValue = nextFn(value);
    return longPipe(tail(fns), newValue);
  }
  return value;
});

const addOne = v => v + 1;

// 10 add ones
const pipeline = longPipe([
  addOne,
  addOne,
  addOne,
  addOne,
  addOne,
  addOne,
  addOne,
  addOne,
  addOne,
  addOne
]);

const shouldBe10 = pipeline(0);

console.log(shouldBe10);

comments powered by Disqus