Skip to content

flip

Swap the only two arguments of a function

63 bytes

Usage

Return a new function that swaps the only two arguments of the original function. This is most useful for reversing the order of a “comparator” (i.e. a function used for sorting).

import * as
import _
_
from 'radashi'
const
const subtract: (a: number, b: number) => number
subtract
= (
a: number
a
: number,
b: number
b
: number) =>
a: number
a
-
b: number
b
const subtract: (a: number, b: number) => number
subtract
(1, 2) // => -1
import _
_
.
flip<[a: number, b: number], number>(fn: (a: number, b: number) => number): (args_0: number, args_1: number) => number
export flip

Flip the first two arguments of a function.

@seehttps://radashi.js.org/reference/curry/flip

@example

const subtract = (x: number, y: number) => x - y
// Equivalent to “y - x”
const flippedSubtract = flip(subtract)
flippedSubtract(3, 4)
// => 1

@version12.2.0

flip
(
const subtract: (a: number, b: number) => number
subtract
)(1, 2) // => 1

Note that functions with more than two arguments are not supported.