forked from a-synchronous/rubico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
287 lines (280 loc) · 8.88 KB
/
map.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const MappingIterator = require('./_internal/MappingIterator')
const MappingAsyncIterator = require('./_internal/MappingAsyncIterator')
const isArray = require('./_internal/isArray')
const isGeneratorFunction = require('./_internal/isGeneratorFunction')
const isAsyncGeneratorFunction = require('./_internal/isAsyncGeneratorFunction')
const arrayMap = require('./_internal/arrayMap')
const generatorFunctionMap = require('./_internal/generatorFunctionMap')
const asyncGeneratorFunctionMap = require('./_internal/asyncGeneratorFunctionMap')
const reducerMap = require('./_internal/reducerMap')
const stringMap = require('./_internal/stringMap')
const setMap = require('./_internal/setMap')
const mapMap = require('./_internal/mapMap')
const objectMap = require('./_internal/objectMap')
const arrayMapSeries = require('./_internal/arrayMapSeries')
const arrayMapPool = require('./_internal/arrayMapPool')
const arrayMapWithIndex = require('./_internal/arrayMapWithIndex')
const symbolIterator = require('./_internal/symbolIterator')
/**
* @name map
*
* @synopsis
* ```coffeescript [specscript]
* Functor<T> = Array<T>|Object<T>|Set<T>|Map<T>
* |Iterator<T>|AsyncIterator<T>|{ map: (T=>any)=>any }
* Reducer<T> = (any, T)=>Promise|any
*
* var T any,
* mapper T=>Promise|any,
* functor Functor<T>
* args ...any,
* generatorFunction ...args=>Generator<T>,
* asyncGeneratorFunction ...args=>AsyncGenerator<T>,
* reducer Reducer<T>
*
* map(mapper)(functor) -> Promise|Functor
*
* map(mapper)(generatorFunction) -> ...args=>Generator
*
* map(mapper)(asyncGeneratorFunction) -> ...args=>AsyncGenerator
*
* map(mapper)(reducer) -> Reducer
* ```
*
* @description
* Apply a mapper concurrently to each item of a functor, returning the results in a functor of the same type. If order is implied by the collection, it is maintained in the result. The following list describes `map` behavior with vanilla JavaScript functors.
*
* * `Array` - apply a mapper to items, returning a new array of results
* * `Object` - apply a mapper to object values, returning a new object of results
* * `Set` - apply a mapper to Set items, returning a new `Set` of results
* * `Map` - apply a mapper to Map values (not entries), returning a new `Map` of results
* * `Iterator`/`Generator` - return an iterator that applies a mapper to each iteration's value, yielding mapped iterations
* * `AsyncIterator`/`AsyncGenerator` - return an async iterator that applies a mapper to each async iteration's value, yielding Promises of a mapped iterations
* * `{ map: function }` - call `.map` directly with mapper
*
* ```javascript [playground]
* const square = number => number ** 2
*
* console.log(
* map(square)([1, 2, 3, 4, 5]),
* ) // [1, 4, 9, 16, 25]
*
* console.log(
* map(square)(new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5]])),
* ) // Map { 'a' => 1, 'b' => 4, 'c' => 9, 'd' => 16, 'e' => 25 }
*
* console.log(
* map(square)({ a: 1, b: 2, c: 3, d: 4, e: 5 }),
* ) // { a: 1, b: 4, c: 9, d: 16, e: 25 }
* ```
*
* `map` recognizes three types of functions in functor position:
*
* * Generator Functions `function* () {}` - `map(mapper)(generatorFunction)` creates a generator function that generates generators of mapped values. Async mappers are yielded synchronously and may lead to unexpected results here.
* * Async Generator Functions `async function* () {}` - `map(mapper)(asyncGeneratorFunction)` creates an async generator function that generates async generators of mapped values. Promises produced by async mappers are resolved.
* * Reducers `(accumulator, item)=>accumulator` - `map(mapper)(reducer)` creates a transducer that, when called with another reducer, creates a mapping step for each item of the reducer's reducing operation. Promises produced by async mappers are resolved.
*
* Use mapping generator functions to create lazy computations executed at iteration time.
*
* ```javascript [playground]
* const capitalize = string => string.toUpperCase()
*
* const abc = function* () {
* yield 'a'; yield 'b'; yield 'c'
* }
*
* const ABC = map(capitalize)(abc)
*
* const abcIter = abc()
*
* const ABCIter = ABC()
*
* console.log([...abcIter]) // ['a', 'b', 'c']
*
* console.log([...ABCIter]) // ['A', 'B', 'C']
* ```
*
* Create a mapping transducer by supplying `map` with a reducer. A reducer is a variadic function that depicts a relationship between an accumulator and any number of arguments. A transducer is a function that accepts a reducer as an argument and returns another reducer.
*
* ```coffeescript [specscript]
* Reducer<T> = (any, T)=>Promise|any
*
* Transducer = Reducer=>Reducer
* ```
*
* The transducer signature enables chaining functionality for reducers. `map` is core to this mechanism, and provides a way via transducers to transform items of reducers. To `map`, reducers are just another category.
*
* ```javascript [playground]
* const square = number => number ** 2
*
* const concat = (array, item) => array.concat(item)
*
* const mapSquare = map(square)
* // mapSquare could potentially be a transducer, but at this point, it is
* // undifferentiated and not necessarily locked in to transducer behavior.
*
* console.log(
* mapSquare([1, 2, 3, 4, 5]),
* ) // [1, 4, 9, 16, 25]
*
* const squareConcatReducer = mapSquare(concat)
* // now mapSquare is passed the function concat, so it assumes transducer
* // position. squareConcatReducer is a reducer with chained functionality
* // square and concat.
*
* console.log(
* [1, 2, 3, 4, 5].reduce(squareConcatReducer, []),
* ) // [1, 4, 9, 16, 25]
*
* console.log(
* [1, 2, 3, 4, 5].reduce(squareConcatReducer, ''),
* ) // '1491625'
* ```
*
* @execution concurrent
*
* @transducing
*
* @TODO streamMap
*/
const map = mapper => function mapping(value) {
if (isArray(value)) {
return arrayMap(value, mapper)
}
if (typeof value == 'function') {
if (isGeneratorFunction(value)) {
return generatorFunctionMap(value, mapper)
}
if (isAsyncGeneratorFunction(value)) {
return asyncGeneratorFunctionMap(value, mapper)
}
return reducerMap(value, mapper)
}
if (value == null) {
return value
}
if (typeof value.next == 'function') {
return symbolIterator in value
? MappingIterator(value, mapper)
: MappingAsyncIterator(value, mapper)
}
if (typeof value == 'string' || value.constructor == String) {
return stringMap(value, mapper)
}
if (value.constructor == Set) {
return setMap(value, mapper)
}
if (value.constructor == Map) {
return mapMap(value, mapper)
}
if (value.constructor == Object) {
return objectMap(value, mapper)
}
return typeof value.map == 'function' ? value.map(mapper) : mapper(value)
}
/**
* @name map.series
*
* @synopsis
* ```coffeescript [specscript]
* var T any,
* mapper T=>Promise|any,
* array Array<T>
*
* map.series(mapper)(array) -> Promise|Array
* ```
*
* @description
* `map` with serial execution.
*
* ```javascript [playground]
* const delayedLog = number => new Promise(function (resolve) {
* setTimeout(function () {
* console.log(number)
* resolve()
* }, 1000)
* })
*
* console.log('start')
* map.series(delayedLog)([1, 2, 3, 4, 5])
* ```
*
* @execution series
*/
map.series = mapper => function mappingInSeries(value) {
if (isArray(value)) {
return arrayMapSeries(value, mapper)
}
throw new TypeError(`${value} is not an Array`)
}
/**
* @name map.pool
*
* @synopsis
* ```coffeescript [specscript]
* var maxConcurrency number,
* T any,
* mapper T=>Promise|any,
* array Array<T>
*
* map.pool(maxConcurrency, mapper)(array) -> Promise|Array
* ```
*
* @description
* `map` with limited concurrency.
*
* ```javascript [playground]
* const delayedLog = x => new Promise(function (resolve) {
* setTimeout(function () {
* console.log(x)
* resolve()
* }, 1000)
* })
*
* console.log('start')
* map.pool(2, delayedLog)([1, 2, 3, 4, 5])
* ```
*
* @execution concurrent
*/
map.pool = (concurrencyLimit, mapper) => function concurrentPoolMapping(value) {
if (isArray(value)) {
return arrayMapPool(value, mapper, concurrencyLimit)
}
throw new TypeError(`${value} is not an Array`)
}
/**
* @name map.withIndex
*
* @synopsis
* ```coffeescript [specscript]
* var T any,
* index number,
* array Array<T>,
* indexedMapper (T, index, array)=>Promise|any
*
* map.withIndex(indexedMapper)(array) -> Promise|Array
* ```
*
* @description
* `map` with an indexed mapper.
*
* ```javascript [playground]
* const range = length => map.withIndex(
* (_, index) => index + 1)(Array(length))
*
* console.log(range(5)) // [1, 2, 3, 4, 5]
* ```
*
* @execution concurrent
*
* @related
* map, filter.withIndex
*/
map.withIndex = mapper => function mappingWithIndex(value) {
if (isArray(value)) {
return arrayMapWithIndex(value, mapper)
}
throw new TypeError(`${value} is not an Array`)
}
module.exports = map