Skip to main content

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