Super simple functional programming library
npm install plura --save
- monads...
Check tests for more usage information
- always
- ap
- chain
- compose
- curry
- curryDestructed
- curryObject
- filter
- find
- isArray
- isNumber
- isObject
- isString
- map
- pipe
- reduce
- reduceObjIndexed
const getTen = always(10) // >> function
getTen() // >> 10
- value
Function and if executed get value
ap([addTwo, minusOne], [1, 2, 3]) // >> [3, 4, 5, 0, 1, 2]
- fns
- array of functions
- values
- array of values
Array of applied functions to a array of values
Aka: flatMap
chain(duplicate, [1, 2]) // >> [1, 1, 2, 2]
- fn
- function to change values
- array
- array to change
Array
const minusOneTimesTo = compose(timeTwo, minusOne)
minusOneTimesTo(2) // >> 2
- fns
- as many functions you want
- arguments
- initial arguments
Composed functions
const addC = curry(add)
addC(1, 1, 1) // >> 3
addC(1)(1)(1) // >> 3
addC(1)(1, 1) // >> 3
- fn
- function you want to curry
- arguments
- as many arguments needed to fulfill fn parameters
Curry:ed function
// add must have simple destruction! : no rest ... or nested destructions
const add = ({ a, b, c}) => a + b + c
const addC = curryDestructed(add)
addC({a: 1, b: 1, c: 1}) // >> 3
addC({a: 1})({b: 1})({c: 1}) // >> 3
addC({c: 1})({b: 1, a: 1}) // >> 3
- function you want to curry
- arguments
- as many arguments needed to fulfill fn parameters
Curry:ed function
// with object
const objectTemplate = {
a: '',
b: '',
c: '',
}
const addC = curryObject(objectTemplate, add)
addC({a: 1, b: 1, c: 1}) // >> 3
addC({a: 1})({b: 1})({c: 1}) // >> 3
addC({c: 1})({b: 1, a: 1}) // >> 3
// with array
const arrayTemplate = [
'a',
'b',
'c',
]
const addC = curryObject(arrayTemplate, add)
addC({a: 1, b: 1, c: 1}) // >> 3
addC({a: 1})({b: 1})({c: 1}) // >> 3
addC({c: 1})({b: 1, a: 1}) // >> 3
- template
- template of object keys, object|array
- fn
- function you want to curry
- arguments
- as many arguments needed to fulfill fn parameters
Curry:ed function
filter(isEven, [2, 3, 6]) // >> [2]
- fn
- function you want to apply to check array elements
- array
- array to filter over
Altred array
find(hasIdOfOne, [{ id: 10 }, { id: 1 }]) // >> { id: 1 }
- fn
- function to evaluate values
- array
- array to find value in
- initailValue
- optional
- if find doesn't find anything initialValue will be returned, default: undefined
Any, found value
isArray([1, 2]) // >> true
- any
- checks if array or not
Boolean
isNumber(10) // >> true
- any
- checks if number or not
Boolean
isObject({ x: 1 }) // >> true
- any
- checks if object or not
Boolean
isString('abc') // >> true
- any
- checks if string or not
Boolean
map(addOne, [1, 2, 3]) // >> [2, 3, 4]
- fn
- function you want to apply to array elements
- array
- array to map over
Altred array
const minusOneTimesTo = pipe(timeTwo, minusOne)
minusOneTimesTo(2) // >> 3
- fns
- as many functions you want
- arguments
- initial arguments
Composed functions
reduce(merge, 0, [2, 3, 6]) // >> 9
- fn
- function you want to apply on array elements
- initialValue
- start value for your reduce
- array || object
- array || object to reduce over
Any
const addToArray = (a, [key, value]) => a.concat(key, value)
reduceObjIndexed(addToArray, [], {x: 2, y: 4}) // >> ['x', 2, 'y', 4]
- fn
- function you want to apply on array elements
- initialValue
- start value for your reduce
- object
- object to reduce over
Any