Skip to content Skip to sidebar Skip to footer

Ramda Pipe With Dynamic Function

var arr = [functionA(), functionB(), functionC()] var data = { /** some data */ } How can I dynamically add the functions from the arr list and add it to ramda pipe. Expected code

Solution 1:

You can use apply:

const buildPipe = apply(pipe);
const add3 = buildPipe([inc, inc, inc]);
const add4 = buildPipe([inc, inc, inc, inc]);

console.log(add3(1));
console.log(add4(2));
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script><script>const {apply, pipe, inc} = R;</script>

Solution 2:

Just use the es6 spread operator which will spread out the array items as arguments to the pipe function.

Also, don't call your functions in the array unless they are returning a function that you want :)

const arr = [functionA, functionB, functionC]
constdata = { /** some data */ }

const newFn = pipe(...arr)

newFn(data)

Post a Comment for "Ramda Pipe With Dynamic Function"