-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathreduce.js
202 lines (198 loc) · 6.68 KB
/
reduce.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const isPromise = require('./_internal/isPromise')
const __ = require('./_internal/placeholder')
const curry3 = require('./_internal/curry3')
const genericReduce = require('./_internal/genericReduce')
// _reduce(collection any, reducer function, initialValue function|any) -> Promise
const _reduce = function (collection, reducer, initialValue) {
if (typeof initialValue == 'function') {
const actualInitialValue = initialValue(collection)
return isPromise(actualInitialValue)
? actualInitialValue.then(curry3(genericReduce, collection, reducer, __))
: genericReduce(collection, reducer, actualInitialValue)
}
return isPromise(initialValue)
? initialValue.then(curry3(genericReduce, collection, reducer, __))
: genericReduce(collection, reducer, initialValue)
}
/**
* @name reduce
*
* @synopsis
* ```coffeescript [specscript]
* type Foldable = Array|Object|Map|Iterator|AsyncIterator
*
* type Reducer = (
* accumulator any,
* value any,
* indexOrKey? number|string,
* collection? Foldable,
* )=>(nextAccumulator Promise|any)
*
* type Resolver = (collection Foldable)=>Promise|any
*
* reduce(
* collection Foldable,
* reducer Reducer,
* initialValue? Resolver|any
* ) -> result Promise|any
*
* reduce(
* reducer Reducer,
* initialValue? Resolver|any
* )(collection Foldable) -> result Promise|any
* ```
*
* @description
* Transforms a collection based on a reducer function and optional initial value. In a reducing operation, the result is defined in the beginning as either the initial value if supplied or the first item of the collection. The reducing operation then iterates through the remaining items in the collection, executing the reducer at each iteration to return the result to be used in the next iteration. The final result is the result of the execution of the reducer at the last item of the iteration. `reduce` accepts the following collections:
*
* * `Array`
* * `Object`
* * `Set`
* * `Map`
* * `Iterator`/`Generator`
* * `AsyncIterator`/`AsyncGenerator`
*
* For arrays (type `Array`), `reduce` executes the reducer function for each item of the array in order, returning a new result at each execution to be used in the next execution. On each iteration, the reducer is passed the accumulator, the item of the iteration, the index of the item in the array, and a reference to the original array.
*
* ```javascript [playground]
* const max = (a, b) => a > b ? a : b
*
* console.log(
* reduce([1, 3, 5, 4, 2], max)
* ) // 5
*
* console.log(
* reduce(max)([1, 3, 5, 4, 2])
* ) // 5
* ```
*
* If an optional initial value is provided, the result starts as the provided initial value rather than the first item of the collection.
*
* ```javascript [playground]
* const add = (a, b) => a + b
*
* console.log(reduce([1, 2, 3, 4, 5], add, 0)) // 15
* console.log(reduce(add, 0)([1, 2, 3, 4, 5])) // 15
* ```
*
* If the initialization parameter is a function, it is treated as a resolver and called with the arguments to resolve the initial value.
*
* ```javascript [playground]
* const concatSquares = (array, value) => array.concat(value ** 2)
*
* const contrivedInitializer = array => [`initial length ${array.length}`]
*
* const array = [1, 2, 3, 4, 5]
*
* console.log(reduce(concatSquares, contrivedInitializer)(array))
* // ['initial length 5', 1, 4, 9, 16, 25]
* console.log(reduce(array, concatSquares, contrivedInitializer))
* // ['initial length 5', 1, 4, 9, 16, 25]
* ```
*
* For objects (type `Object`), `reduce` executes the reducer function for each value of the object. On each iteration, the reducer is passed the accumulator, the object value, the key of the object value, and a reference to the original object.
*
* ```javascript [playground]
* const add = (a, b) => a + b
*
* const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }
*
* console.log(
* reduce(obj, add)
* ) // 15
*
* console.log(
* reduce(add)(obj)
* ) // 15
* ```
*
* For sets (type `Set`), `reduce` executes the reducer function for each item of the set. On each iteration, the reducer is passed the accumulator and item of the set.
*
* ```javascript [playground]
* const add = (a, b) => a + b
*
* const set = new Set([1, 2, 3, 4, 5])
*
* console.log(
* reduce(set, add)
* ) // 15
*
* console.log(
* reduce(add)(set)
* ) // 15
* ```
*
* For maps (type `Map`), `reduce` executes the reducer function for each value of each entry of the map. On each iteration, the reducer is passed the accumulator, the map item, the key of the map item, and a reference to the original map.
*
* ```javascript [playground]
* const add = (a, b) => a + b
*
* const m = new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5]])
*
* console.log(
* reduce(m, add)
* ) // 15
*
* console.log(
* reduce(add)(m)
* ) // 15
* ```
*
* For iterators (type `Iterator`) and generators (type `Generator`), `reduce` executes the reducer function for each value of the iterator/generator. On each iteration, the reducer is passed the accumulator and the item of the iteration. The iterator/generator is consumed in the process.
*
* ```javascript [playground]
* const add = (a, b) => a + b
*
* const generate12345 = function* () {
* yield 1; yield 2; yield 3; yield 4; yield 5
* }
*
* console.log(
* reduce(generate12345(), add)
* ) // 15
*
* console.log(
* reduce(add)(generate12345())
* ) // 15
* ```
*
* For asyncIterators (type `AsyncIterator`) and asyncGenerators (type `AsyncGenerator`), `reduce` executes the reducer function for each value of the asyncIterator/asyncGenerator. On each iteration, the reducer is passed the accumulator and the item of the async iteration. The asyncIterator/asyncGenerator is consumed in the process.
*
* ```javascript [playground]
* const asyncAdd = async (a, b) => a + b
*
* const asyncGenerate12345 = async function* () {
* yield 1; yield 2; yield 3; yield 4; yield 5
* }
*
* reduce(asyncGenerate12345(), asyncAdd).then(console.log) // 15
*
* reduce(asyncAdd)(asyncGenerate12345()).then(console.log) // 15
* ```
*
* Any promises passed in argument position are resolved for their values before further execution. This only applies to the eager version of the API.
*
* ```javascript [playground]
* const add = (a, b) => a + b
*
* reduce(Promise.resolve([1, 2, 3, 4, 5]), add, 0).then(console.log) // 15
* ```
*
* @execution series
*
* @transducing
*
* @TODO readerReduce
*
* @TODO reduce.concurrent
*/
const reduce = function (...args) {
if (typeof args[0] == 'function') {
return curry3(_reduce, __, args[0], args[1])
}
if (isPromise(args[0])) {
return args[0].then(curry3(_reduce, __, args[1], args[2]))
}
return _reduce(args[0], args[1], args[2])
}
module.exports = reduce