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.
Require the core.operators
package, after installing it:
var operators = require('core.operators')
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']
|
core.operators.
subtract
(a, b)¶Number → Number → Number
JavaScript’s subtraction (a - b
) operator.
core.operators.
divide
(a, b)¶Number → Number → Number
JavaScript’s division (a / b
) operator.
core.operators.
multiply
(a, b)¶Number → Number → Number
JavaScript’s multiplication (a * b
) operator.
core.operators.
get
(key, object)¶String → Object → α | Undefined
Property accessor (object[key]
).
core.operators.
has
(key, object)¶String → Object → Boolean
Tests the existence of a property in an object (key in object
).
core.operators.
isInstance
(constructor, a)¶Function → Object → Boolean
Instance check (a instanceof constructor
).