-
Notifications
You must be signed in to change notification settings - Fork 15
/
Transducer.d.ts
231 lines (230 loc) · 7.63 KB
/
Transducer.d.ts
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
/**
* @name Transducer.map
*
* @synopsis
* ```coffeescript [specscript]
* type Reducer = (
* accumulator any,
* value any,
* indexOrKey? number|string,
* collection? Foldable,
* )=>(nextAccumulator Promise|any)
*
* type Transducer = Reducer=>Reducer
*
* Transducer.map(mapperFunc function) -> mappingTransducer Transducer
* ```
*
* @description
* Creates a mapping transducer. Items in the final 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
*
* console.log(
* reduce([1, 2, 3, 4, 5], squareConcatReducer, [])
* ) // [1, 4, 9, 16, 25]
*
* // the same squareConcatReducer is consumable with vanilla JavaScript
* console.log(
* [1, 2, 3, 4, 5].reduce(squareConcatReducer, [])
* ) // [1, 4, 9, 16, 25]
*
* // concat is implicit when transforming into arrays
* console.log(
* transform([1, 2, 3, 4, 5], Transducer.map(square), [])
* ) // [1, 4, 9, 16, 25]
* ```
*
* Read more on transducers [here](/blog/transducers-crash-course-rubico-v2).
*/
export function map(mapper: any): (arg0: any) => any;
/**
* @name Transducer.filter
*
* @synopsis
* ```coffeescript [specscript]
* type Reducer = (
* accumulator any,
* value any,
* indexOrKey? number|string,
* collection? Foldable,
* )=>(nextAccumulator Promise|any)
*
* type Transducer = Reducer=>Reducer
*
* Transducer.filter(predicate function) -> filteringTransducer Transducer
* ```
*
* @description
* Creates a filtering transducer. A filtering reducer skips items of reducing operation if they test falsy 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 = filter(isOdd)(concat)
*
* console.log(
* [1, 2, 3, 4, 5].reduce(concatOddNumbers, []),
* ) // [1, 3, 5]
* ```
*/
export function filter(predicate: any): (arg0: any) => any;
/**
* @name Transducer.flatMap
*
* @synopsis
* ```coffeescript [specscript]
* type Reducer = (
* accumulator any,
* value any,
* indexOrKey? number|string,
* collection? Foldable,
* )=>(nextAccumulator Promise|any)
*
* type Transducer = Reducer=>Reducer
*
* Transducer.flatMap(flatMapper) -> flatMappingTransducer Transducer
* ```
*
* @description
* Creates a flatMapping transducer. A flatMapping transducer applies the flatMapping function to each item of the reducing operation, concatenating the results of the flatMapper execution into the final result. 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]
*
* console.log(
* transform(numbers, Transducer.flatMap(powers), [])
* ) // [1, 1, 1, 2, 4, 8, 3, 9, 27, 4, 16, 64, 5, 25, 125]
* ```
*
* Read more on transducers [here](/blog/transducers-crash-course-rubico-v2).
*/
export function flatMap(flatMapper: any): (arg0: any) => any;
/**
* @name Transducer.forEach
*
* @synopsis
* ```coffeescript [specscript]
* type Reducer = (
* accumulator any,
* value any,
* indexOrKey? number|string,
* collection? Foldable,
* )=>(nextAccumulator Promise|any)
*
* type Transducer = Reducer=>Reducer
*
* Transducer.forEach(func function) -> forEachTransducer Transducer
* ```
*
* @description
* Creates an effectful pasthrough transducer. The effectful passthrough transducer applies the effectful function to each item of the reducing operation, leaving the reducing operation unchanged. It is possible to use an asynchronous effectful 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), // 1 4 9 16 25
* ]), null)
* ```
*/
export function forEach(func: any): (arg0: any) => any;
/**
* @name Transducer.passthrough
*
* @synopsis
* ```coffeescript [specscript]
* type Reducer = (
* accumulator any,
* value any,
* indexOrKey? number|string,
* collection? Foldable,
* )=>(nextAccumulator Promise|any)
*
* type Transducer = Reducer=>Reducer
*
* Transducer.passthrough(func function) -> passthroughTransducer Transducer
* ```
*
* @description
* Creates a pasthrough transducer. The passthrough transducer passes each item of the reducing operation through, leaving the reducing operation unchanged.
*
* ```javascript [playground]
* const createAsyncNumbers = async function* () {
* let number = 0
* while (number < 10) {
* yield number
* number += 1
* }
* }
*
* transform(createAsyncNumbers(), Transducer.passthrough, [])
* .then(console.log) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
* ```
*/
export function passthrough(reducer: any): any;
/**
* @name Transducer.tryCatch
*
* @synopsis
* ```coffeescript [specscript]
* type Reducer = (
* accumulator any,
* item any,
* indexOrKey? number|string,
* collection? Foldable,
* )=>(nextAccumulator Promise|any)
*
* type Transducer = Reducer=>Reducer
*
* Transducer.tryCatch(
* transducerTryer Transducer,
* catcher (error Error, item any)=>Promise|any,
* ) -> tryCatchTransducer Transducer
* ```
*
* @description
* Creates an error handling transducer. 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 as well as the original item (before any processing by the transducer) 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: 'George' })
* 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)
* ```
*/
export function tryCatch(transducerTryer: any, catcher: any): (arg0: any) => any;