Reputation: 753
I'm new to functional programming and have a question regarding pipe.
Say I have the following function:
const modifyArr = R.curry((i, newValue, arr) =>
Object.assign([], arr, {[i]: newValue}))
This function change an array's value on a specific index.
Now, I want to use this function twice (with pipe) in order to implement swap between two element in the array.
I cannot figured out how to write it. This is what I got:
const swap = (arr, a, b) =>
R.pipe(modifyArr(b, arr[a], modifyArr(a, arr[b])))
Notice that the curried version of modifyArr (after receiving two arguments) expects an array, but I'm passing 3 arguments to the swap function.
How can I use pipe where my parent function receive a different arguments than the first function in the pipe?
Thanks.
Upvotes: 0
Views: 278
Reputation: 665040
Given that your functions are curried, you can simply partially apply the former arguments before doing the piping:
const swap = (arr, a, b) => R.pipe(modifyArr(a, arr[b]), modifyArr(b, arr[a]))(arr);
Upvotes: 2