Skip to content

flip

Creates a function that invokes fn with the first two arguments swapped.

Useful when adapting a function for use in higher-order pipelines where the argument order is reversed (e.g. passing a binary callback to reduce).

Available since v2.0.0

import { flip } from '@helpers4/function';
flip<A, B, Rest extends unknown[], R>(fn: function): function
ParameterTypeDescription
fnfunctionThe function to wrap.

function — A new function with the first two parameters swapped.

Returns a new function where the first two arguments are swapped.

const sub = (a: number, b: number) => a - b;
flip(sub)(3, 10); // => 7  (10 - 3)

Useful for adapting binary callbacks in higher-order functions.

const divide = (a: number, b: number) => a / b;
const divideInto = flip(divide);
divideInto(2, 100); // => 50

View source on GitHub