jonshot
jonshot

Reputation: 805

Applying a list of functions to a list of arguments using Ramda

Is there a function in Ramda to apply a list of functions to a list of arguments similar to juxt() but where each function only gets supplied with the argument at the corresponding index in the list of arguments? Something similar to:

pipe(zip, map(([a, b]) => a(b)))([func1, func2], ['arg1', 'arg2']);

Is there a better way to do this?

Upvotes: 1

Views: 418

Answers (1)

Ori Drori
Ori Drori

Reputation: 191936

Use R.zipWith which creates a new list out of the two supplied by applying the function to each equally-positioned pair in the lists.

You can also replace the callback with R.call, which invokes the first argument, and passes the rest as it's parameters.

const { zipWith, call } = R;

const fn = zipWith(call);

const func1 = R.add(1);
const func2 = R.add(2);

const result = fn([func1, func2], [1, 2]);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous"></script>

Upvotes: 4

Related Questions