Reverse arguments order in curried function (ramda js)

Viewed 3836

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

2 Answers

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)
Related