flip()
flip<
First,Second,Rest,Result>(fn): (b,a, ...rest) =>Result
Creates a function with its first two arguments reversed.
Type Parametersβ
First: Firstβ
The first argument type.
Second: Secondβ
The second argument type.
Rest: Rest extends unknown[]β
The remaining argument types.
Result: Resultβ
The return type.
Parametersβ
fn: (a, b, ...rest) => Resultβ
The function to flip.
Returnsβ
The flipped function.
Sinceβ
2.0.0
Also known asβ
flip (Lodash, es-toolkit, Remeda, Radashi, Ramda, Effect) Β· β (Modern Dash, Antfu)
Exampleβ
const divide = (a: number, b: number) => a / b;
const divideBy = flip(divide);
divide(10, 2); // => 5
divideBy(2, 10); // => 5
How it works?β
Creates a function with its first two arguments reversed.
Argument Swapβ
Use Casesβ
Create natural APIs πβ
Reverse function parameters to create more intuitive and readable function interfaces. Essential for API design and improving code readability.
const greet = (name, greeting) => `${greeting}, ${name}!`;
const greetReverse = flip(greet);
greetReverse("Hello", "World"); // "Hello, World!" (flipped)
Enable currying patternsβ
Reverse parameters to enable currying and partial application for functional programming. Critical for functional programming and creating reusable function components.
const map = (fn, array) => array.map(fn);
const mapFlipped = flip(map);
// Now data comes last, easier to curry or pipe
const doubleAll = (arr) => mapFlipped(arr, n => n * 2);