-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathTransducer.js
More file actions
292 lines (284 loc) · 10.7 KB
/
Transducer.js
File metadata and controls
292 lines (284 loc) · 10.7 KB
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
288
289
290
291
292
const funcConcat = require('./_internal/funcConcat')
const reducerMap = require('./_internal/reducerMap')
const reducerFilter = require('./_internal/reducerFilter')
const reducerFlatMap = require('./_internal/reducerFlatMap')
const reducerForEach = require('./_internal/reducerForEach')
const reducerTryCatch = require('./_internal/reducerTryCatch')
const curry2 = require('./_internal/curry2')
const curry3 = require('./_internal/curry3')
const __ = require('./_internal/placeholder')
/**
* @name Transducer
*
* @description
* Temporary repository of transducer functionality throughout rubico v1
*/
const Transducer = {}
/**
* @name Transducer.map
*
* @synopsis
* ```coffeescript [specscript]
* type SyncOrAsyncReducer = (accumulator any, value any)=>(nextAccumulator Promise|any)
* type Transducer = SyncOrAsyncReducer=>SyncOrAsyncReducer
* type UnarySyncOrAsyncMapper = (item any)=>(mappedItem Promise|any)
*
* Transducer.map(mapper UnarySyncOrAsyncMapper) -> mappingTransducer Transducer
* ```
*
* @description
* Creates a mapping [transducer](/blog/transducers-crash-course). Items of the transducer's reducing operation are transformed by the mapper function. It is possible to use an asynchronous mapper, however the reducing operation must support asynchronous execution. This library provides such implementations as [reduce](/docs/reduce) and [transform](/docs/transform).
*
* ```javascript [playground]
* const square = number => number ** 2
*
* const concat = (array, item) => array.concat(item)
*
* const mapSquare = Transducer.map(square)
* // mapSquare is a transducer
*
* const squareConcatReducer = mapSquare(concat)
* // now mapSquare is passed the reducer function concat; squareConcatReducer
* // is a reducer with chained functionality square and concat
*
* const squaredNumbersRubicoReduce = reduce([1, 2, 3, 4, 5], squareConcatReducer, [])
* console.log(squaredNumbersRubicoReduce)
*
* // the same squareConcatReducer is consumable with vanilla JavaScript
* const squaredNumbersVanillaReduce = [1, 2, 3, 4, 5].reduce(squareConcatReducer, [])
* console.log(squaredNumbersVanillaReduce)
*
* // concat is implicit when transforming into arrays
* const squaredNumbersTransform = transform([1, 2, 3, 4, 5], Transducer.map(square), [])
* console.log(squaredNumbersTransform)
* ```
*
* See also:
* * [thunkify](/docs/thunkify)
* * [Transducer.filter](/docs/Transducer.filter)
* * [Transducer.flatMap](/docs/Transducer.flatMap)
* * [Transducer.forEach](/docs/Transducer.forEach)
* * [Transducer.passthrough](/docs/Transducer.passthrough)
* * [Transducer.tryCatch](/docs/Transducer.tryCatch)
*
*/
Transducer.map = function transducerMap(mapper) {
return curry2(reducerMap, __, mapper)
}
/**
* @name Transducer.filter
*
* @synopsis
* ```coffeescript [specscript]
* type SyncOrAsyncReducer = (accumulator any, value any)=>(nextAccumulator Promise|any)
* type Transducer = SyncOrAsyncReducer=>SyncOrAsyncReducer
* type UnarySyncOrAsyncPredicate = any=>Promise|boolean|any
*
* Transducer.filter(predicate UnarySyncOrAsyncPredicate) -> filteringTransducer Transducer
* ```
*
* @description
* Creates a filtering [transducer](/blog/transducers-crash-course). A filtering transducer filters out items of its reducing operation if they test false by the predicate. It is possible to use an asynchronous predicate, however the reducing operation must support asynchronous execution. This library provides such implementations as [reduce](/docs/reduce) and [transform](/docs/transform).
*
* ```javascript [playground]
* const isOdd = number => number % 2 == 1
*
* const concat = (array, item) => array.concat(item)
*
* const concatOddNumbers = Transducer.filter(isOdd)(concat)
*
* const array = [1, 2, 3, 4, 5]
*
* const oddNumbers1 = array.reduce(concatOddNumbers, [])
* const oddNumbers2 = transform(array, Transducer.filter(isOdd), [])
*
* console.log(oddNumbers1)
* console.log(oddNumbers2)
* ```
*
* See also:
* * [thunkify](/docs/thunkify)
* * [Transducer.map](/docs/Transducer.map)
* * [Transducer.flatMap](/docs/Transducer.flatMap)
* * [Transducer.forEach](/docs/Transducer.forEach)
* * [Transducer.passthrough](/docs/Transducer.passthrough)
* * [Transducer.tryCatch](/docs/Transducer.tryCatch)
*
*/
Transducer.filter = function transducerFilter(predicate) {
return curry2(reducerFilter, __, predicate)
}
/**
* @name Transducer.flatMap
*
* @synopsis
* ```coffeescript [specscript]
* type SyncOrAsyncReducer = (accumulator any, value any)=>(nextAccumulator Promise|any)
* type Transducer = SyncOrAsyncReducer=>SyncOrAsyncReducer
* type Monad = Array|String|Set|Generator|AsyncGenerator|{ flatMap: string }|{ chain: string }|Object
* type UnarySyncOrAsyncFlatMapper = (item any)=>(monad Promise|Monad|any)
*
* Transducer.flatMap(flatMapper UnarySyncOrAsyncFlatMapper) -> flatMappingTransducer Transducer
* ```
*
* @description
* Creates a flatMapping [transducer](/blog/transducers-crash-course). A flatMapping transducer applies the flatMapper function to each item of its reducing operation, concatenating the results of the flatMapper execution onto the accumulator. It is possible to use an asynchronous flatMapper, however the reducing operation must support asynchronous execution. This library provides such implementations as [reduce](/docs/reduce) and [transform](/docs/transform).
*
* ```javascript [playground]
* const powers = number => [number, number ** 2, number ** 3]
*
* const numbers = [1, 2, 3, 4, 5]
*
* const result = transform(numbers, Transducer.flatMap(powers), [])
*
* console.log(result)
* ```
*
* See also:
* * [thunkify](/docs/thunkify)
* * [Transducer.map](/docs/Transducer.map)
* * [Transducer.filter](/docs/Transducer.filter)
* * [Transducer.forEach](/docs/Transducer.forEach)
* * [Transducer.passthrough](/docs/Transducer.passthrough)
* * [Transducer.tryCatch](/docs/Transducer.tryCatch)
*
*/
Transducer.flatMap = function transducerFlatMap(flatMapper) {
return curry2(reducerFlatMap, __, flatMapper)
}
/**
* @name Transducer.forEach
*
* @synopsis
* ```coffeescript [specscript]
* type SyncOrAsyncReducer = (accumulator any, value any)=>(nextAccumulator Promise|any)
* type Transducer = SyncOrAsyncReducer=>SyncOrAsyncReducer
* type UnarySyncOrAsyncCallback = (item any)=>Promise|undefined
*
* Transducer.forEach(callback UnarySyncOrAsyncCallback) -> forEachTransducer Transducer
* ```
*
* @description
* Creates an iterative [transducer](/blog/transducers-crash-course). Executes a callback function for each item of a reducing operation, leaving each item unmodified. It is possible to use an asynchronous callback function, however the reducing operation must support asynchronous execution. This library provides such implementations as [reduce](/docs/reduce) and [transform](/docs/transform).
*
* ```javascript [playground]
* const numbers = [1, 2, 3, 4, 5]
*
* transform(numbers, compose(
* Transducer.map(number => number ** 2),
* Transducer.forEach(console.log),
* ), null)
* ```
*
* See also:
* * [thunkify](/docs/thunkify)
* * [Transducer.map](/docs/Transducer.map)
* * [Transducer.filter](/docs/Transducer.filter)
* * [Transducer.flatMap](/docs/Transducer.flatMap)
* * [Transducer.passthrough](/docs/Transducer.passthrough)
* * [Transducer.tryCatch](/docs/Transducer.tryCatch)
*
*/
Transducer.forEach = function transducerForEach(func) {
return curry2(reducerForEach, __, func)
}
/**
* @name Transducer.passthrough
*
* @synopsis
* ```coffeescript [specscript]
* type SyncOrAsyncReducer = (accumulator any, value any)=>(nextAccumulator Promise|any)
* type Transducer = SyncOrAsyncReducer=>SyncOrAsyncReducer
*
* Transducer.passthrough -> Transducer
* ```
*
* @description
* Creates a pasthrough [transducer](/blog/transducers-crash-course). The passthrough transducer simply passes each item of the reducing operation through to the next downstream transducer, leaving each item unmodified.
*
* ```javascript [playground]
* const createAsyncNumbers = async function* () {
* let number = 0
* while (number < 10) {
* yield number
* number += 1
* }
* }
*
* const numbers = await transform(createAsyncNumbers(), Transducer.passthrough, [])
*
* console.log(numbers)
* ```
*
* See also:
* * [thunkify](/docs/thunkify)
* * [Transducer.map](/docs/Transducer.map)
* * [Transducer.filter](/docs/Transducer.filter)
* * [Transducer.flatMap](/docs/Transducer.flatMap)
* * [Transducer.forEach](/docs/Transducer.forEach)
* * [Transducer.tryCatch](/docs/Transducer.tryCatch)
*
*/
Transducer.passthrough = function transducerPassthrough(reducer) {
return reducer
}
/**
* @name Transducer.tryCatch
*
* @synopsis
* ```coffeescript [specscript]
* type SyncOrAsyncReducer = (accumulator any, value any)=>(nextAccumulator Promise|any)
* type Transducer = SyncOrAsyncReducer=>SyncOrAsyncReducer
*
* Transducer.tryCatch(
* transducerTryer Transducer,
* catcher (error Error, item any)=>(Promise|any)
* ) -> tryCatchTransducer Transducer
* ```
*
* @description
* Creates an error handling [transducer](/blog/transducers-crash-course). The error handling transducer wraps a transducer and catches any errors thrown by the transducer with the catcher function. The catcher function is provided the error and the item for which the error was thrown. It is possible for either the transducer or the catcher to be asynchronous, however the reducing operation must support asynchronous execution. This library provides such implementations as [reduce](/docs/reduce) and [transform](/docs/transform).
*
* ```javascript [playground]
* const db = new Map()
* db.set('a', { id: 'a', name: 'John' })
* db.set('b', { id: 'b', name: 'Jane' })
* db.set('c', { id: 'c', name: 'Jill' })
* db.set('e', { id: 'e', name: 'Jim' })
*
* const userIds = ['a', 'b', 'c', 'd', 'e']
*
* transform(userIds, Transducer.tryCatch(
* compose(
* Transducer.map(async userId => {
* if (db.has(userId)) {
* return db.get(userId)
* }
* throw new Error(`user ${userId} not found`)
* }),
*
* Transducer.forEach(user => {
* console.log('Found', user.name)
* })
* ),
* (error, userId) => {
* console.error(error)
* console.log('userId in catcher:', userId)
* // original userId for which the error was thrown is provided
* }
* ), null)
* ```
*
* See also:
* * [thunkify](/docs/thunkify)
* * [Transducer.map](/docs/Transducer.map)
* * [Transducer.filter](/docs/Transducer.filter)
* * [Transducer.flatMap](/docs/Transducer.flatMap)
* * [Transducer.forEach](/docs/Transducer.forEach)
* * [Transducer.passthrough](/docs/Transducer.passthrough)
*
*/
Transducer.tryCatch = function transducerTryCatch(transducerTryer, catcher) {
return curry3(reducerTryCatch, __, transducerTryer, catcher)
}
module.exports = Transducer