Reputation: 169
I have a higher order function: let's say for simplicity
const divideLeftToRight = x => y => x/y;
I would like to have a function that performs the division but from 'right to left'. In other words, I need to have:
const divideRightToLeft = x => y => y/x;
I thought about doing:
const divideRightToLeft = R.curry((x,y) => divideLeftToRight(y)(x));
I am wondering if there is a more elegant way to do it
Upvotes: 5
Views: 4442
Reputation: 2761
You could uncurry the function before flipping. (flip
returns a curried function.)
E.g.
const divideRightToLeft = R.flip(R.uncurryN(2, divideLeftToRight))
Alternatively, you can define a custom flipCurried
function:
// https://github.com/gcanti/fp-ts/blob/15f4f701ed2ba6b0d306ba7b8ca9bede223b8155/src/function.ts#L127
const flipCurried = <A, B, C>(fn: (a: A) => (b: B) => C) => (b: B) => (a: A) => fn(a)(b);
const divideRightToLeft = flipCurried(divideLeftToRight)
Upvotes: 2
Reputation: 665040
You are looking for the flip
function:
const divideRightToLeft = R.flip(divideLeftToRight)
Upvotes: 7