fp-array
is a group of functions to do declarative programming in a more convenient way than in Array.prototype
$ npm install fp-array
const map = require('fp-array').map
const filter = require('fp-array').filter
const reduce = require('fp-array').reduce
const arr = [...]
map(arr)
.with(callback)
[.and(callback2)]
[...]
.result()
filter(arr)
.is(callback)
[.and(callback2)]
[...]
.result()
reduce(arr, callback[, initialValue])
const arr = [1, 2, 3]
// callbacks
const increment = n => n + 1
const double = n => n * 2
map(arr)
.with(increment)
.and(double)
.and(String)
.result() // ['4', '6', '8']
arr // [1, 2, 3]
- Iterates only one time over
arr
- Inmutable:
arr
has not mutated, a new Array is returned whenresult()
- Declarative: Easy to read syntax
const arr = [1, 'a', true, 6, 0, 4, undefined, 10]
const number = n => typeof n === 'number'
const greaterThan4 = n => n > 4
const even = n => n % 2 === 0
filter(arr)
.is(number)
.and(even)
.and(greaterThan4)
.result() // [6, 10]
arr // [1, 'a', true, 6, 0, 4, undefined, 10]
Same notes than before applies.
const arr = [1, 2, 3]
reduce(
arr,
(acc, currentValue) => acc + currentValue,
5
) // 11
reduce()
works exactly like the original except now is outside the Array.prototype
More examples in tests.
Open an issue or say hi on twitter: @juliomatcom
Copyright (c) 2016 Julio César Martín