Module: core.operators

Stability:3 - Stable
Bug Tracker:https://github.com/folktale/core.operators/issues
Version:1.0.0
Repository:https://github.com/folktale/core.operators
Portability:Portable
npm package:core.operators

Provides JS operators as curried functions.

Loading

Require the core.operators package, after installing it:

var operators = require('core.operators')

Why?

JavaScript’s operators are not first-class functions, so using them in a higher-order function requires one to wrap the call at the call-site:

1
2
3
4
5
6
7
var people = [
  { name: 'Bob', age: 14 },
  { name: 'Alice', age: 12 }
]

people.map(function(person){ return person.name })
// => ['Bob', 'Alice']

This defeats some of the compositional nature of functional programming. This module provides first-class, curried versions of these special operators that you can combine with the usual function composition operations:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
var op = require('core.operators')
people.map(op.get('name'))
// => ['Bob', 'Alice']

function compare(a, b) {
  return a > b?    1
  :      a === b?  0
  :   /* a < b */ -1
}

var lambda = require('core.lambda')
people.sort(lambda.upon(compare, op.get('age'))).map(op.get('name'))
// => ['Alice', 'Bob']

Arithmetic

add()

core.operators.add(a, b)
Number → Number → Number

JavaScript’s addition (a + b) operator.

subtract()

core.operators.subtract(a, b)
Number → Number → Number

JavaScript’s subtraction (a - b) operator.

divide()

core.operators.divide(a, b)
Number → Number → Number

JavaScript’s division (a / b) operator.

multiply()

core.operators.multiply(a, b)
Number → Number → Number

JavaScript’s multiplication (a * b) operator.

modulus()

core.operators.modulus(a, b)
Number → Number → Number

JavaScript’s modulus (a % b) operator.

negate()

core.operators.negate(a)
Number → Number

JavaScript’s unary negation (-a) operator.

increment()

core.operators.increment(a)
Number → Number

Short for add(1)(a).

decrement()

core.operators.decrement(a)
Number → Number

Short for subtract(a)(1).

Bitwise

bitNot()

core.operators.bitNot(a)
Int → Int

Bitwise negation (~a)

bitAnd()

core.operators.bitAnd(a, b)
Int → Int → Int

Bitwise intersection (a & b)

bitOr()

core.operators.bitOr(a, b)
Int → Int → Int

Bitwise union (a | b)

bitXor()

core.operators.bitXor(a, b)
Int → Int → Int

Bitwise exclusive union (a ^ b)

bitShiftLeft()

core.operators.bitShiftLeft(a, b)
Int → Int → Int

Bitwise left shift (a << b)

bitShiftRight()

core.operators.bitShiftRight(a, b)
Int → Int → Int

Sign-propagating bitwise right shift (a >> b)

bitUnsignedShiftRight()

core.operators.bitUnsignedShiftRight(a, b)
Int → Int → Int

Zero-fill bitwise right shift (a >>> b)

Logical

not()

core.operators.not(a)
Boolean → Boolean

Logical negation (!a).

and()

core.operators.and(a, b)
Boolean → Boolean → Boolean

Logical conjunction (a && b).

or()

core.operators.or(a, b)
Boolean → Boolean → Boolean

Logical disjunction (a || b).

Relational

equal()

core.operators.equal(a, b)
α → α → Boolean

Strict reference equality (a === b).

notEqual()

core.operators.notEqual(a, b)
α → α → Boolean

Strict reference inequality (a !== b).

greaterThan()

core.operators.greaterThan(a, b)
α → α → Boolean

Greater than (a > b).

greaterThanOrEqualTo()

core.operators.greaterThanOrEqualTo(a, b)
α → α → Boolean

Greater than or equal to (a >= b).

lessThan()

core.operators.lessThan(a, b)
α → α → Boolean

Less than (a < b).

lessThanOrEqualTo()

core.operators.lessThanOrEqualTo(a, b)
α → α → Boolean

Less than or equal to (a <= b).

Special

get()

core.operators.get(key, object)
String → Object → α | Undefined

Property accessor (object[key]).

has()

core.operators.has(key, object)
String → Object → Boolean

Tests the existence of a property in an object (key in object).

isInstance()

core.operators.isInstance(constructor, a)
Function → Object → Boolean

Instance check (a instanceof constructor).

create()

core.operators.create(constructor, ...args)
(new(α₁, α₂, ..., αₙ) → β) → (α₁, α₂, ..., αₙ) → β)

Constructs new objects (new constructor(...args))

typeOf()

core.operators.typeOf(a)
α → String

Returns the internal type of the object (typeof a)

classOf()

core.operators.classOf(a)
α → String

Returns the internal [[Class]] of the object.