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