operators

Methods

(static) add(x, y) → {number|string}

Source:

A curried function version of the binary + addition/concatenation operator.

This works with strings as well as with numbers.

console.log(4 + 7)       // 11
console.log(add (4) (7)) // 11

const add4 = add (4)
console.log(add4 (7))    // 11
Parameters:
Name Type Description
x number | string

The first operand.

y number | string

The second operand.

Returns:

The sum of x and y (if numbers) or x and y concatenated (if strings).

Type
number | string

(static) band(x, y) → {number}

Source:

A curried function version of the & bitwise and operator.

console.log(114 & 75)        // 66
console.log(band (114) (75)) // 66

const and114 = band (114)
console.log(and114 (75))     // 66
Parameters:
Name Type Description
x number

The first operand.

y number

The second operand.

Returns:

The bitwise AND of x and y.

Type
number

(static) bls(x, y) → {number}

Source:

A curried function version of the << bitwise left shift operator.

Note that when used with a single argument, that argument is the one on the left side of the <<. If you need the argument to be the one on the right side, you can use the swap combinator on bls. See the examples below.

console.log(114 << 2)      // 456
console.log(bls (114) (2)) // 456

const ls114 = bls (114)
console.log(ls114 (2))     // 456

const lsBy2 = swap (bls) (2)
console.log(lsBy2 (114))   // 456
Parameters:
Name Type Description
x number

The number to be shifted.

y number

The number of bits to left shift x.

Returns:

The result of shifting x to the left by y bits.

Type
number

(static) bnot(x) → {number}

Source:

A function version of the ~ bitwise not operator.

console.log(~114)       // -115
console.log(bnot (114)) // -115
Parameters:
Name Type Description
x number

The operand.

Returns:

The bitwise NOT of x.

Type
number

(static) bor(x, y) → {number}

Source:

A curried function version of the | bitwise or operator.

console.log(114 | 75)       // 123
console.log(bor (114) (75)) // 123

const or114 = bor (114)
console.log(or114 (75))     // 123
Parameters:
Name Type Description
x number

The first operand.

y number

The second operand.

Returns:

The bitwise OR of x and y.

Type
number

(static) brs(x, y) → {number}

Source:

A curried function version of the >> bitwise right shift operator.

Note that when used with a single argument, that argument is the one on the left side of the >>. If you need the argument to be the one on the right side, you can use the swap combinator on brs. See the examples below.

console.log(-114 >> 2)      // -29
console.log(brs (-114) (2)) // -29

const rs114 = brs (-114)
console.log(rs114 (2))      // -29

const rsBy2 = swap (brs) (2)
console.log(rsBy2 (-114))   // -29
Parameters:
Name Type Description
x number

The number to be shifted.

y number

The number of bits to right shift x.

Returns:

The result of shifting x to the right by y bits, shifting the sign bit onto the left.

Type
number

(static) burs(x, y) → {number}

Source:

A curried function version of the >>> bitwise unsigned right shift operator.

The u in burs stands for "unsigned". This is in contrast to brs, which is an signed right shift. The difference is that brs shifts copies of the sign bit onto the left, while burs shifts 0's onto the left.

For positive numbers, signed and unsigned shifts are identical. They can produce some very different results on negative numbers, though.

Note that when used with a single argument, that argument is the one on the left side of the >>>. If you need the argument to be the one on the right side, you can use the swap combinator on burs. See the examples below.

console.log(-114 >>> 2)      // 1073741795
console.log(burs (-114) (2)) // 1073741795

const urs114 = burs (-114)
console.log(urs114 (2))      // 1073741795

const ursBy2 = swap (burs) (2)
console.log(ursBy2 (-114))   // 1073741795
Parameters:
Name Type Description
x number

The number to be shifted.

y number

The number of bits to right shift x.

Returns:

The result of shifting x to the right by y bits, shifting 0's onto the left.

Type
number

(static) bxor(x, y) → {number}

Source:

A curried function version of the ^ bitwise exclusive or operator.

console.log(114 ^ 75)        // 57
console.log(bxor (114) (75)) // 57

const xor114 = bxor (114)
console.log(xor114 (75))     // 57
Parameters:
Name Type Description
x number

The first operand.

y number

The second operand.

Returns:

The bitwise XOR of x and y.

Type
number

(static) ceq(x, y) → {boolean}

Source:

A curried function version of the == coercing equals operator.

You probably mean to use eq instead.

console.log(3 == '3')      // true
console.log(ceq (3) ('3')) // true

const ceq3 = ceq (3)
console.log(ceq3 ('3'))    // true

console.log(3 == 4)        // false
console.log(ceq (3) (4))   // false
console.log(ceq3 (4))      // false
Parameters:
Name Type Description
x *

The first parameter.

y *

The second parameter.

Returns:

Whether the two arguments are equal, with equality defined by ==.

Type
boolean

(static) cne(x, y) → {boolean}

Source:

A curried function version of the != coercing not equals operator.

You probably mean to use ne instead.

console.log(3 != '3')      // false
console.log(cne (3) ('3')) // false

const cne3 = cne (3)
console.log(cne3 ('3'))    // false

console.log(3 != 4)        // true
console.log(cne (3) (4))   // true
console.log(cne3 (4))      // true
Parameters:
Name Type Description
x *

The first parameter.

y *

The second parameter.

Returns:

Whether the two arguments are unequal, with equality defined by ==.

Type
boolean

(static) coal(x, y) → {*}

Source:

A curried function version of the ?? nullish coalescing operator.

Note that when used with a single argument, that argument is the one on the left side of the ??. If you need the argument to be the one on the right side, you can use the swap combinator on coal. See the examples below.

console.log(false ?? true)       // false
console.log(coal (false) (true)) // false

const orDef = swap (coal) ('default')
console.log(orDef (null))        // "default"
console.log(orDef (undefined))   // "default"
console.log(orDef ("value"))     // "value"
Parameters:
Name Type Description
x *

The first operand.

y *

The second operand.

Returns:

If x is not either null or undefined, then x. Otherwise y.

Type
*

(static) cond(x, y, z) → {*}

Source:

A curried function version of the ?: conditional (ternary) operator.

This is a three-parameter function, so the normal way of manipulating argument order for operators (swap) would require multiple calls composed to do what is intended. Instead, rotl and rotr, both of which work with three-parameter functions natively, may be useful. In particular, rotr(cond) will create a function where the conditional argument is last, which may be useful.

console.log(true ? 1 : 0)        // 1
console.log(cond (true) (1) (0)) // 1

const bool2num = rotr (cond) (1) (0)
console.log(bool2num (true))     // 1
console.log(bool2num (false))    // 0
Parameters:
Name Type Description
x boolean

The conditional operand.

y *

The operand that is returned if x evaluates to true.

z *

The operand that is returned if x evaluates to false.

Returns:

Either y or z, depending on whether x is true or false.

Type
*

(static) div(x, y) → {number}

Source:

A curried function version of the / division operator.

Note that when used with a single argument, that argument is the dividend (the number that gets divided). If you need the argument to be the divisor (the number by which the dividend is divided), use the swap combinator. See the examples below.

console.log(4 / 7)              // 0.5714285714285714
console.log(div (4) (7))        // 0.5714285714285714

const div4 = div (4)
console.log(div4 (7))           // 0.5714285714285714

console.log(swap (div) (4) (7)) // 1.75

const dividedBy4 = swap (div) (4)
console.log(dividedBy4 (7))     // 1.75
Parameters:
Name Type Description
x number

The dividend.

y number

The divisor.

Returns:

The quotient of x and y.

Type
number

(static) divBy(x, y) → {number}

Source:

A curried function version of the / division operator, but in reverse order.

This is the same function as div except that the arguments are reversed. The motivation is that division isn't commutative, and it's extremely common to divide an unknown number by a known number. A function to halve an argument would look like this with div:

const halve = swap (div) (2)

while with divBy it would instead be:

const halve = divBy (2)

Thus divBy allows common functions to be written without the extra complexity of adding swap.

Parameters:
Name Type Description
x number

The divisor.

y number

The dividend.

Returns:

The quotient of y and x.

Type
number

(static) eq(x, y) → {boolean}

Source:

A curried function version of the === equals operator.

console.log(3 === '3')    // false
console.log(eq (3) ('3')) // false

const eq3 = eq (3)
console.log(eq3 ('3'))    // false

console.log(3 === 3)      // true
console.log(eq (3) (3))   // true
console.log(eq3 (3))      // true
Parameters:
Name Type Description
x *

The first parameter.

y *

The second parameter.

Returns:

Whether the two arguments are equal, with equality defined by ===.

Type
boolean

(static) ge(x, y) → {boolean}

Source:

A curried function version of the >= greater than or equal operator.

Note that when used with a single argument, that argument is the one on the left side of the >=. If you need the argument to be the one on the right side, you can either swap the arguments and use le or use the swap combinator on ge. See the examples below.

console.log(4 >= 3)        // true
console.log(ge (4) (3))    // true

const geFrom4 = ge(4)
// This is the same as x => 4 >= x
console.log(geFrom4 (3))   // true

// To do x => x >= 4, there are two choices
const ge4a = swap (ge) (4) // Actual x => x >= 4
const ge4b = le (4)        // x => 4 <= x is the same thing
console.log(ge4a (3))      // false
console.log(ge4b (3))      // false
Parameters:
Name Type Description
x *

The first parameter.

y *

The second parameter.

Returns:

Whether x is greater than or equal to y.

Type
boolean

(static) gt(x, y) → {boolean}

Source:

A curried function version of the > greater than operator.

Note that when used with a single argument, that argument is the one on the left side of the >. If you need the argument to be the one on the right side, you can either swap the arguments and use lt or use the swap combinator on gt. See the examples below.

console.log(4 > 3)         // true
console.log(gt (4) (3))    // true

const gtFrom4 = gt (4)
// This is the same as x => 4 > x
console.log(gtFrom4 (3))   // true

// To do x => x > 4, there are two choices
const gt4a = swap (gt) (4) // Actual x => x > 4
const gt4b = lt (4)        // x => 4 < x is the same thing
console.log(gt4a (3))      // false
console.log(gt4b (3))      // false
Parameters:
Name Type Description
x *

The first parameter.

y *

The second parameter.

Returns:

Whether x is greater than y.

Type
boolean

(static) land(x, y) → {*}

Source:

A curried function version of the && logical and operator.

Note that, while this function still has the short-circuiting behavior of the && operator, it still requires both arguments be provided even if the first is enough to determine the outcome. In other words, land(false) will still need a second argument before it provides its answer, even though enough is there to determine that the return value will be false and that the second argument won't even be evaluated when provided.

console.log(true && false)       // false
console.log(land (true) (false)) // false

// This will result in `false` no matter the second argument, but the
// second argument is still necessary
const andFalse = land (false)
console.log(andFalse (true))     // false
Parameters:
Name Type Description
x *

The first operand.

y *

The second operand.

Returns:

The logical AND of x and y. It will return the first argument if it evaluates to false, otherwise it'll return the second argument.

Type
*

(static) le(x, y) → {boolean}

Source:

A curried function version of the <= less than or equals operator.

Note that when used with a single argument, that argument is the one on the left side of the <=. If you need the argument to be the one on the right side, you can either swap the arguments and use ge or use the swap combinator on le. See the examples below.

console.log(3 <= 4)        // true
console.log(le (3) (4))    // true

const leFrom3 = le(3)
// This is the same as x => 3 <= x
console.log(leFrom3 (4))   // true

// To do x => x <= 3, there are two choices
const le3a = swap (le) (3) // Actual x => x <= 3
const le3b = ge (3)        // x => 3 >= x is the same thing
console.log(le3a (4))      // false
console.log(le3b (4))      // false
Parameters:
Name Type Description
x *

The first parameter.

y *

The second parameter.

Returns:

Whether x is less than or equal to y.

Type
boolean

(static) lnot(x) → {number}

Source:

A function version of the ! logical not operator.

console.log(!0)          // true
console.log(lnot (0))    // true

console.log(!true)       // false
console.log(lnot (true)) // false
Parameters:
Name Type Description
x number

The operand.

Returns:

The logical NOT of x. It will return true if the argument evaluates to false and false otherwise.

Type
number

(static) lor(x, y) → {number}

Source:

A curried function version of the || logical or operator.

Note that, while this function still has the short-circuiting behavior of the || operator, it still requires both arguments be provided even if the first is enough to determine the outcome. In other words, lor(true) will still need a second argument before it provides its answer, even though enough is there to determine that the return value will be true and that the second argument won't even be evaluated when provided.

console.log(true || false)      // true
console.log(lor (true) (false)) // true

// This will result in `true` no matter the second argument, but the
// second argument is still necessary
const orTrue = lor (true)
console.log(orTrue (false))     // true
Parameters:
Name Type Description
x number

The first operand.

y number

The second operand.

Returns:

The logical OR of x and y. It will return the first argument if it evaluates to true, otherwise it'll return the second argument.

Type
number

(static) lt(x, y) → {boolean}

Source:

A curried function version of the < less than operator.

Note that when used with a single argument, that argument is the one on the left side of the <. If you need the argument to be the one on the right side, you can either swap the arguments and use gt or use the swap combinator on lt. See the examples below.

console.log(3 < 4)         // true
console.log(lt (3) (4))    // true

const ltFrom3 = lt(3)
// This is the same as x => 3 < x
console.log(ltFrom3 (4))   // true

// To do x => x < 3, there are two choices
const lt3a = swap (lt) (3) // Actual x => x < 3
const lt3b = gt (3)        // x => 3 > x is the same thing
console.log(lt3a (4))      // false
console.log(lt3b (4))      // false
Parameters:
Name Type Description
x *

The first parameter.

y *

The second parameter.

Returns:

Whether x is less than y.

Type
boolean

(static) mul(x, y) → {number}

Source:

A curried function version of the * multiplication operator.

console.log(4 * 7)       // 28
console.log(mul (4) (7)) // 28

const mul4 = mul (4)
console.log(mul4 (7))    // 28
Parameters:
Name Type Description
x number

The first operand.

y number

The second operand.

Returns:

The product of x and y.

Type
number

(static) ne(x, y) → {boolean}

Source:

A curried function version of the !== not equals operator.

console.log(3 !== '3')    // true
console.log(ne (3) ('3')) // true

const ne3 = ne (3)
console.log(ne3 ('3'))    // true

console.log(3 !== 3)      // false
console.log(ne (3) (3))   // false
console.log(ne3 (3))      // false
Parameters:
Name Type Description
x *

The first parameter.

y *

The second parameter.

Returns:

Whether the two arguments are unequal, with equality defined by ===.

Type
boolean

(static) neg(x)

Source:

A function version of the unary - negation operator.

console.log(-4)      // -4
console.log(neg (4)) // -4
Parameters:
Name Type Description
x number

The number to negate.

Returns:

The negated number.

(static) opt(x, y) → {*}

Source:

A curried function version of the ?.[] optional chaining operator.

The first argument is the object upon which a property will be looked up. The second argument is a property name (a string or a symbol) or an index (a number). The function will return either the value of a property with that name, the value of an array at the given index, or undefined if the first argument itself is null or undefined.

If you need to partially apply the property name/index, then use swap in conjunction with opt. See the examples below.

Note that this function only performs optional chaining property access. It does not do optional chaining function application (?.()).

const a = [1, 2, 3, 4, 5]
const b = null

console.log(a[1])               // 2
console.log(opt (a) (1))        // 2

console.log(a.length)           // 5
console.log(opt (a) ('length')) // 5

console.log(b?.length)          // undefined
console.log(opt (b) ('length')) // undefined

const count = swap (opt) ('length')
console.log(count (a))          // 5
console.log(count (b))          // undefined
Parameters:
Name Type Description
x object | null

The object to look up the property name or index on.

y string | symbol | number

The name of the property or the index to look up.

Returns:

The value of index y or the property with the name y in x. If x is null or undefined, then undefined is returned.

Type
*

(static) plus(x)

Source:

A function version of the unary + plus operator.

console.log(+'3')       // 3
console.log(pos ('3'))  // 3

console.log(+true)      // 1
console.log(pos (true)) // 1
Parameters:
Name Type Description
x *

The value to convert to a number.

Returns:

The number that x was converted to, or NaN if it could not be converted.

(static) pow(x, y) → {number}

Source:

A curried function version of the ** exponentiation operator.

Note that when used with a single argument, that argument is the base (the number that is raised to a power). If you need the argument to be the exponent (the power that the base is raised to), use the swap combinator. See the examples below.

console.log(2 ** 5)             // 32
console.log(pow (2) (5))        // 32

const powerOf2 = pow (2)
console.log(powerOf2 (5))       // 32

console.log(swap (pow) (2) (5)) // 25

const square = swap (pow) (2)
console.log(square (5))         // 25
Parameters:
Name Type Description
x number

The base.

y number

The exponent.

Returns:

The base raised to the power of the exponent.

Type
number

(static) prop(x, y) → {*}

Source:

A curried function version of the [] property access operator, but in reverse order.

The first argument in this case is the name of the property or the numerical index (if the object is an array), and the second is the object that property/index is being looked up on. This is the opposite order of propOf and is included because it is very common to look up a property of a particular name and have the object vary.

As an example, this is a function that determines the length of an array argument, using the regular-order propOf:

const length = swap (propOf) ('length')

while this is the same function using the reverse-order prop:

const length = prop ('length')

Not requiring swap simplifies the definition, and since this is an extremely common pattern, this extra function has been provided for it.

The other common situation where swap gets used very often is in comparison operators, but reverse-order operators already exist. For example, swap (lt) (3) is the same as ge (3).

Parameters:
Name Type Description
x string | symbol | number

The name of the property or the index to look up.

y object

The object to look up the property name or index on.

Returns:

The value of index x or the property with the name x in y.

Type
*

(static) propOf(x, y) → {*}

Source:

A curried function version of the [] property access operator.

The first argument is the object upon which a property will be looked up. The second argument is a property name (a string or a symbol) or an index (a number). The function will return either the value of a property with that name or the value of an array at the given index.

If you need to partially apply the property name/index, then use swap in conjunction with prop. See the examples below.

const a = [1, 2, 3, 4, 5]

console.log(a[1])                // 2
console.log(propOf (a) (1))        // 2

console.log(a.length)            // 5
console.log(propOf (a) ('length')) // 5

const count = swap (propOf) ('length')
console.log(count (a))           // 5
Parameters:
Name Type Description
x object

The object to look up the property name or index on.

y string | symbol | number

The name of the property or the index to look up.

Returns:

The value of index y or the property with the name y in x.

Type
*

(static) rem(x, y) → {number}

Source:

A curried function version of the % remainder operator.

Note that when used with a single argument, that argument is the dividend (the number that gets divided). If you need the argument to be the divisor (the number by which the dividend is divided), use the swap combinator. See the examples below.

console.log(4 % 7)              // 4
console.log(rem (4) (7))        // 4

const rem4 = rem (4)
console.log(rem4 (7))           // 4

console.log(swap (rem) (4) (7)) // 3

const remWith4 = swap (rem) (4)
console.log(remWith4 (7))       // 3
Parameters:
Name Type Description
x number

The dividend.

y number

The divisor.

Returns:

The remainder left when x is divided by y.

Type
number

(static) remBy(x, y) → {number}

Source:

A curried function version of the % remainder operator, but in reverse order.

This is the same function as rem except that the arguments are reversed. The motivation is that division isn't commutative, and it's extremely common to want the remainder of an unknown number divided by a known number. A function to see if an argument is even would look like this with rem:

const even = eq (0) (swap (rem) (2))

while with remBy it would instead be:

const even = eq (0) (remBy (2))

Thus remBy allows common functions to be written without the extra complexity of adding swap.

Parameters:
Name Type Description
x number

The divisor.

y number

The dividend.

Returns:

The remainder left when y is divided by x.

Type
number

(static) sub(x, y) → {number}

Source:

A curried function version of the binary - subtraction operator.

Note that when used with a single argument, that argument is the minuend (the number from which the other is subtracted). If you need the argument to be the subtrahend (the number that is subtracted from the other number), use the swap combinator. See the examples below.

console.log(4 - 7)              // -3
console.log(sub (4) (7))        // -3

const sub4 = sub (4)
console.log(sub4 (7))           // -3

console.log(swap (sub) (4) (7)) // 3

const minus4 = swap (sub) (4)
console.log(minus4 (7))         // 3
Parameters:
Name Type Description
x number

The minuend.

y number

The subtrahend.

Returns:

The difference of x and y.

Type
number

(static) subBy(x, y) → {number}

Source:

A curried function version of the binary - subtraction operator, but in reverse order.

This is the same function as sub except that the arguments are reversed. The motivation is that subtraction isn't commutative, and it's extremely common to subtract an unknown number by a known number. A function to decrement an argument would look like this with sub:

const dec = swap (sub) (1)

while with subBy it would instead be:

const dec = subBy (1)

Thus subBy allows common functions to be written without the extra complexity of adding swap.

Parameters:
Name Type Description
x number

The subtrahend.

y number

The minuend.

Returns:

The difference of y and x.

Type
number