-
Notifications
You must be signed in to change notification settings - Fork 8
/
reductions.js
31 lines (26 loc) · 919 Bytes
/
reductions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"use strict";
var reduce = require("reducible/reduce")
var reducible = require("reducible/reducible")
var end = require("reducible/end")
var isError = require("reducible/is-error")
function reductions(source, f, initial) {
/**
Returns `reducible` collection of the intermediate values of the reduction
(as per reduce) of `source` by `f`, starting with `initial` value.
## Example
var numbers = reductions([1, 1, 1, 1], function(accumulated, value) {
return accumulated + value
}, 0)
print(numbers) // => < 1 2 3 4 >
**/
return reducible(function reduceReductions(next, start) {
var state = initial
return reduce(source, function reduceReductionsSource(value, result) {
if (value === end) return next(end, result)
if (isError(value)) return next(value, result)
state = f(state, value)
return next(state, result)
}, start)
})
}
module.exports = reductions