@rimbu/common 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bun/async-optlazy.mts +18 -5
- package/dist/bun/internal.mts +0 -2
- package/dist/bun/optlazy.mts +12 -3
- package/dist/cjs/async-optlazy.cjs +4 -4
- package/dist/cjs/comp.cjs +21 -911
- package/dist/cjs/index.cjs +23 -910
- package/dist/cjs/internal.cjs +23 -910
- package/dist/cjs/optlazy.cjs +2 -2
- package/dist/esm/async-optlazy.mjs +10 -4
- package/dist/esm/async-optlazy.mjs.map +1 -1
- package/dist/esm/comp.mjs.map +1 -1
- package/dist/esm/eq.mjs.map +1 -1
- package/dist/esm/index-range.mjs.map +1 -1
- package/dist/esm/internal.mjs +0 -2
- package/dist/esm/internal.mjs.map +1 -1
- package/dist/esm/optlazy.mjs +7 -2
- package/dist/esm/optlazy.mjs.map +1 -1
- package/dist/esm/range.mjs.map +1 -1
- package/dist/esm/update.mjs.map +1 -1
- package/dist/types/async-optlazy.d.mts +10 -3
- package/dist/types/internal.d.mts +0 -2
- package/dist/types/optlazy.d.mts +8 -2
- package/package.json +3 -3
- package/src/async-optlazy.mts +18 -5
- package/src/internal.mts +0 -2
- package/src/optlazy.mts +12 -3
- package/dist/bun/async-reducer.mts +0 -1157
- package/dist/bun/reducer.mts +0 -1025
- package/dist/cjs/async-reducer.cjs +0 -1621
- package/dist/cjs/reducer.cjs +0 -1621
- package/dist/esm/async-reducer.mjs +0 -638
- package/dist/esm/async-reducer.mjs.map +0 -1
- package/dist/esm/reducer.mjs +0 -669
- package/dist/esm/reducer.mjs.map +0 -1
- package/dist/types/async-reducer.d.mts +0 -542
- package/dist/types/reducer.d.mts +0 -542
- package/src/async-reducer.mts +0 -1157
- package/src/reducer.mts +0 -1025
|
@@ -1,542 +0,0 @@
|
|
|
1
|
-
import { type AsyncCollectFun, AsyncOptLazy, type MaybePromise, Reducer, Eq } from './internal.mjs';
|
|
2
|
-
/**
|
|
3
|
-
* An `AsyncReducer` is a stand-alone asynchronous calculation that takes input values of type I,
|
|
4
|
-
* and, when requested, produces an output value of type O.
|
|
5
|
-
* @typeparam I - the input value type
|
|
6
|
-
* @typeparam O - the output value type
|
|
7
|
-
*/
|
|
8
|
-
export type AsyncReducer<I, O = I> = AsyncReducer.Impl<I, O, unknown>;
|
|
9
|
-
export declare namespace AsyncReducer {
|
|
10
|
-
interface Impl<I, O, S> {
|
|
11
|
-
/**
|
|
12
|
-
* The initial state value for the reducer algorithm.
|
|
13
|
-
*/
|
|
14
|
-
readonly init: AsyncOptLazy<S>;
|
|
15
|
-
/**
|
|
16
|
-
* Returns the next state based on the given input values
|
|
17
|
-
* @param state - the current state
|
|
18
|
-
* @param elem - the current input value
|
|
19
|
-
* @param index - the current input index
|
|
20
|
-
* @param halt - a function that, when called, ensures no more values are passed to the reducer
|
|
21
|
-
*/
|
|
22
|
-
next(state: S, elem: I, index: number, halt: () => void): MaybePromise<S>;
|
|
23
|
-
/**
|
|
24
|
-
* Returns the output value based on the given `state`
|
|
25
|
-
* @param state - the current state
|
|
26
|
-
*/
|
|
27
|
-
stateToResult(state: S): MaybePromise<O>;
|
|
28
|
-
/**
|
|
29
|
-
* An optional function that is called when the reducer will no longer receive values.
|
|
30
|
-
* @param state - the final reducer state
|
|
31
|
-
* @param error - (optional) if an error has occured, it ix passed here
|
|
32
|
-
*/
|
|
33
|
-
onClose?: ((state: S, error?: unknown) => MaybePromise<void>) | undefined;
|
|
34
|
-
/**
|
|
35
|
-
* Returns an `AsyncReducer` instance that only passes values to the reducer that satisy the given `pred` predicate.
|
|
36
|
-
* @param pred - a potaentially asynchronous function that returns true if the value should be passed to the reducer based on the following inputs:<br/>
|
|
37
|
-
* - value: the current input value<br/>
|
|
38
|
-
* - index: the current input index<br/>
|
|
39
|
-
* - halt: function that, when called, ensures no more new values are passed to the reducer
|
|
40
|
-
* @example
|
|
41
|
-
* ```ts
|
|
42
|
-
* AsyncReducer
|
|
43
|
-
* .createMono(0, async (c, v) => c + v)
|
|
44
|
-
* .filterInput(async v => v > 10)
|
|
45
|
-
* // this reducer will only sum values larger than 10
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
filterInput(pred: (value: I, index: number, halt: () => void) => MaybePromise<boolean>): AsyncReducer<I, O>;
|
|
49
|
-
/**
|
|
50
|
-
* Returns an `AsyncReducer` instance that converts its input values using given `mapFun` before passing them to the reducer.
|
|
51
|
-
* @param mapFun - a potentially asynchronous function that returns a new value to pass to the reducer based on the following inputs:<br/>
|
|
52
|
-
* - value: the current input value<br/>
|
|
53
|
-
* - index: the current input index
|
|
54
|
-
* @example
|
|
55
|
-
* ```ts
|
|
56
|
-
* AsyncReducer
|
|
57
|
-
* .createMono(0, async (c, v) => c + v)
|
|
58
|
-
* .mapInput(async v => v * 2)
|
|
59
|
-
* // this reducer will double all input values before summing them
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
|
-
mapInput<I2>(mapFun: (value: I2, index: number) => MaybePromise<I>): AsyncReducer<I2, O>;
|
|
63
|
-
/**
|
|
64
|
-
* Returns an `AsyncReducer` instance that converts or filters its input values using given `collectFun` before passing them to the reducer.
|
|
65
|
-
* @param collectFun - a function receiving<br/>
|
|
66
|
-
* - `value`: the next value<br/>
|
|
67
|
-
* - `index`: the value index<br/>
|
|
68
|
-
* - `skip`: a token that, when returned, will not add a value to the resulting collection<br/>
|
|
69
|
-
* - `halt`: a function that, when called, ensures no next elements are passed
|
|
70
|
-
* @example
|
|
71
|
-
* ```ts
|
|
72
|
-
* AsyncReducer
|
|
73
|
-
* .createMono(0, async (c, v) => c + v)
|
|
74
|
-
* .collectInput(async (v, _, skip) => v <= 10 ? skip : v * 2)
|
|
75
|
-
* // this reducer will double all input values larger thant 10 before summing them,
|
|
76
|
-
* // and will skip all values smaller than 10
|
|
77
|
-
* ```
|
|
78
|
-
*/
|
|
79
|
-
collectInput<I2>(collectFun: AsyncCollectFun<I2, I>): AsyncReducer<I2, O>;
|
|
80
|
-
/**
|
|
81
|
-
* Returns an `AsyncReducer` instance that converts its output values using given `mapFun`.
|
|
82
|
-
* @param mapFun - a potentially asynchronous function that takes the current output value and converts it to a new output value
|
|
83
|
-
* @example
|
|
84
|
-
* ```ts
|
|
85
|
-
* AsyncReducer
|
|
86
|
-
* .createMono(0, async (c, v) => c + v)
|
|
87
|
-
* .mapOutput(async v => String(v))
|
|
88
|
-
* // this reducer will convert all its results to string before returning them
|
|
89
|
-
* ```
|
|
90
|
-
*/
|
|
91
|
-
mapOutput<O2>(mapFun: (value: O) => O2): AsyncReducer<I, O2>;
|
|
92
|
-
mapOutput<O2>(mapFun: (value: O) => Promise<O2>): AsyncReducer<I, O2>;
|
|
93
|
-
/**
|
|
94
|
-
* Returns an `AsyncReducer` instance that takes at most the given `amount` of input elements, and will ignore subsequent elements.
|
|
95
|
-
* @param amount - the amount of elements to accept
|
|
96
|
-
* @example
|
|
97
|
-
* ```ts
|
|
98
|
-
* await AsyncStream
|
|
99
|
-
* .from(Stream.range({ end: 10 }))
|
|
100
|
-
* .reduce(
|
|
101
|
-
* AsyncReducer
|
|
102
|
-
* .createMono(0, async (c, v) => c + v)
|
|
103
|
-
* .takeInput(2)
|
|
104
|
-
* )
|
|
105
|
-
* // => 1
|
|
106
|
-
* ```
|
|
107
|
-
*/
|
|
108
|
-
takeInput(amount: number): AsyncReducer<I, O>;
|
|
109
|
-
/**
|
|
110
|
-
* Returns a `Reducer` instance that skips the first given `amount` of input elements, and will process subsequent elements.
|
|
111
|
-
* @param amount - the amount of elements to skip
|
|
112
|
-
* @example
|
|
113
|
-
* ```ts
|
|
114
|
-
* await AsyncStream
|
|
115
|
-
* .from(Stream.range({ end: 10 }))
|
|
116
|
-
* .reduce(
|
|
117
|
-
* AsyncReducer
|
|
118
|
-
* .createMono(0, async (c, v) => c + v)
|
|
119
|
-
* .dropInput(9)
|
|
120
|
-
* )
|
|
121
|
-
* // => 19
|
|
122
|
-
* ```
|
|
123
|
-
*/
|
|
124
|
-
dropInput(amount: number): AsyncReducer<I, O>;
|
|
125
|
-
/**
|
|
126
|
-
* Returns a `Reducer` instance that takes given `amount` of elements starting at given `from` index, and ignores other elements.
|
|
127
|
-
* @param from - (default: 0) the index at which to start processing elements
|
|
128
|
-
* @param amount - (optional) the amount of elements to process, if not given, processes all elements from the `from` index
|
|
129
|
-
* @example
|
|
130
|
-
* ```ts
|
|
131
|
-
* await AsyncStream
|
|
132
|
-
* .from(Stream.range({ end: 10 }))
|
|
133
|
-
* .reduce(
|
|
134
|
-
* AsyncReducer
|
|
135
|
-
* .createMono(0, async (c, v) => c + v)
|
|
136
|
-
* .sliceInput(1, 2)
|
|
137
|
-
* )
|
|
138
|
-
* // => 3
|
|
139
|
-
* ```
|
|
140
|
-
*/
|
|
141
|
-
sliceInput(from?: number, amount?: number): AsyncReducer<I, O>;
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* A base class that can be used to easily create `AsyncReducer` instances.
|
|
145
|
-
* @typeparam I - the input value type
|
|
146
|
-
* @typeparam O - the output value type
|
|
147
|
-
* @typeparam S - the internal state type
|
|
148
|
-
*/
|
|
149
|
-
class Base<I, O, S> implements AsyncReducer.Impl<I, O, S> {
|
|
150
|
-
readonly init: AsyncOptLazy<S>;
|
|
151
|
-
readonly next: (state: S, elem: I, index: number, halt: () => void) => MaybePromise<S>;
|
|
152
|
-
readonly stateToResult: (state: S) => MaybePromise<O>;
|
|
153
|
-
readonly onClose?: ((state: S, error?: unknown) => MaybePromise<void>) | undefined;
|
|
154
|
-
constructor(init: AsyncOptLazy<S>, next: (state: S, elem: I, index: number, halt: () => void) => MaybePromise<S>, stateToResult: (state: S) => MaybePromise<O>, onClose?: ((state: S, error?: unknown) => MaybePromise<void>) | undefined);
|
|
155
|
-
filterInput(pred: (value: I, index: number, halt: () => void) => MaybePromise<boolean>): AsyncReducer<I, O>;
|
|
156
|
-
mapInput<I2>(mapFun: (value: I2, index: number) => MaybePromise<I>): AsyncReducer<I2, O>;
|
|
157
|
-
collectInput<I2>(collectFun: AsyncCollectFun<I2, I>): AsyncReducer<I2, O>;
|
|
158
|
-
mapOutput<O2>(mapFun: (value: O) => MaybePromise<O2>): AsyncReducer<I, O2>;
|
|
159
|
-
takeInput(amount: number): AsyncReducer<I, O>;
|
|
160
|
-
dropInput(amount: number): AsyncReducer<I, O>;
|
|
161
|
-
sliceInput(from?: number, amount?: number): AsyncReducer<I, O>;
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Returns an `AsyncReducer` with the given options:
|
|
165
|
-
* @param init - the optionally lazy and/or promised initial state value
|
|
166
|
-
* @param next - returns (potentially asynchronously) the next state value based on the given inputs:<br/>
|
|
167
|
-
* - current: the current state<br/>
|
|
168
|
-
* - next: the current input value<br/>
|
|
169
|
-
* - index: the input index value<br/>
|
|
170
|
-
* - halt: function that, when called, ensures no more elements are passed to the reducer
|
|
171
|
-
* @param stateToResult - a potentially asynchronous function that converts the current state to an output value
|
|
172
|
-
* @param onClose - (optional) a function that will be called when the reducer will no longer receive values
|
|
173
|
-
* @typeparam I - the input value type
|
|
174
|
-
* @typeparam O - the output value type
|
|
175
|
-
* @typeparam S - the internal state type
|
|
176
|
-
*/
|
|
177
|
-
function create<I, O = I, S = O>(init: AsyncOptLazy<S>, next: (current: S, next: I, index: number, halt: () => void) => MaybePromise<S>, stateToResult: (state: S) => MaybePromise<O>, onClose?: (state: S, error?: unknown) => MaybePromise<void>): AsyncReducer<I, O>;
|
|
178
|
-
/**
|
|
179
|
-
* Returns an `AsyncReducer` of which the input, state, and output types are the same.
|
|
180
|
-
* @param init - the optionally lazy and/or promised initial state value
|
|
181
|
-
* @param next - returns (potentially asynchronously) the next state value based on the given inputs:<br/>
|
|
182
|
-
* - current: the current state<br/>
|
|
183
|
-
* - next: the current input value<br/>
|
|
184
|
-
* - index: the input index value<br/>
|
|
185
|
-
* - halt: function that, when called, ensures no more elements are passed to the reducer
|
|
186
|
-
* @param stateToResult - a potentially asynchronous function that converts the current state to an output value
|
|
187
|
-
* @param onClose - (optional) a function that will be called when the reducer will no longer receive values
|
|
188
|
-
* @typeparam I - the input value type
|
|
189
|
-
* @typeparam O - the output value type
|
|
190
|
-
* @typeparam S - the internal state type
|
|
191
|
-
*/
|
|
192
|
-
function createMono<T>(init: AsyncOptLazy<T>, next: (current: T, next: T, index: number, halt: () => void) => MaybePromise<T>, stateToResult?: (state: T) => MaybePromise<T>, onClose?: (state: T, error?: unknown) => MaybePromise<void>): AsyncReducer<T>;
|
|
193
|
-
/**
|
|
194
|
-
* Returns an `AsyncReducer` of which the state and output types are the same.
|
|
195
|
-
* @param init - the optionally lazy and/or promised initial state value
|
|
196
|
-
* @param next - returns (potentially asynchronously) the next state value based on the given inputs:<br/>
|
|
197
|
-
* - current: the current state<br/>
|
|
198
|
-
* - next: the current input value<br/>
|
|
199
|
-
* - index: the input index value<br/>
|
|
200
|
-
* - halt: function that, when called, ensures no more elements are passed to the reducer
|
|
201
|
-
* @param stateToResult - a potentially asynchronous function that converts the current state to an output value
|
|
202
|
-
* @param onClose - (optional) a function that will be called when the reducer will no longer receive values
|
|
203
|
-
* @typeparam I - the input value type
|
|
204
|
-
* @typeparam O - the output value type
|
|
205
|
-
* @typeparam S - the internal state type
|
|
206
|
-
*/
|
|
207
|
-
function createOutput<I, O = I>(init: AsyncOptLazy<O>, next: (current: O, next: I, index: number, halt: () => void) => MaybePromise<O>, stateToResult?: (state: O) => MaybePromise<O>, onClose?: (state: O, error?: unknown) => MaybePromise<void>): AsyncReducer<I, O>;
|
|
208
|
-
function from<I, O>(reducer: Reducer<I, O>): AsyncReducer<I, O>;
|
|
209
|
-
/**
|
|
210
|
-
* A `Reducer` that sums all given numeric input values.
|
|
211
|
-
* @example
|
|
212
|
-
* ```ts
|
|
213
|
-
* console.log(Stream.range({ amount: 5 }).reduce(Reducer.sum))
|
|
214
|
-
* // => 10
|
|
215
|
-
* ```
|
|
216
|
-
*/
|
|
217
|
-
const sum: AsyncReducer<number, number>;
|
|
218
|
-
/**
|
|
219
|
-
* A `Reducer` that calculates the product of all given numeric input values.
|
|
220
|
-
* @example
|
|
221
|
-
* ```ts
|
|
222
|
-
* console.log(Stream.range({ start: 1, amount: 5 }).reduce(product))
|
|
223
|
-
* // => 120
|
|
224
|
-
* ```
|
|
225
|
-
*/
|
|
226
|
-
const product: AsyncReducer<number, number>;
|
|
227
|
-
/**
|
|
228
|
-
* A `Reducer` that calculates the average of all given numberic input values.
|
|
229
|
-
* @example
|
|
230
|
-
* ```ts
|
|
231
|
-
* console.log(Stream.range({ amount: 5 }).reduce(Reducer.average));
|
|
232
|
-
* // => 2
|
|
233
|
-
* ```
|
|
234
|
-
*/
|
|
235
|
-
const average: AsyncReducer<number, number>;
|
|
236
|
-
/**
|
|
237
|
-
* Returns a `Reducer` that remembers the minimum value of the inputs using the given `compFun` to compare input values
|
|
238
|
-
* @param compFun - a comparison function for two input values, returning 0 when equal, positive when greater, negetive when smaller
|
|
239
|
-
* @param otherwise - (default: undefineds) a fallback value when there were no input values given
|
|
240
|
-
* @example
|
|
241
|
-
* ```ts
|
|
242
|
-
* const stream = Stream.of('abc', 'a', 'abcde', 'ab')
|
|
243
|
-
* console.log(stream.minBy((s1, s2) => s1.length - s2.length))
|
|
244
|
-
* // 'a'
|
|
245
|
-
* ```
|
|
246
|
-
*/
|
|
247
|
-
const minBy: {
|
|
248
|
-
<T>(compFun: (v1: T, v2: T) => MaybePromise<number>): AsyncReducer<T, T | undefined>;
|
|
249
|
-
<T, O>(compFun: (v1: T, v2: T) => MaybePromise<number>, otherwise: AsyncOptLazy<O>): AsyncReducer<T, T | O>;
|
|
250
|
-
};
|
|
251
|
-
/**
|
|
252
|
-
* Returns a `Reducer` that remembers the minimum value of the numberic inputs.
|
|
253
|
-
* @param otherwise - (default: undefined) a fallback value when there were no input values given
|
|
254
|
-
* @example
|
|
255
|
-
* ```ts
|
|
256
|
-
* console.log(Stream.of(5, 3, 7, 4).reduce(Reducer.min()))
|
|
257
|
-
* // => 3
|
|
258
|
-
* ```
|
|
259
|
-
*/
|
|
260
|
-
const min: {
|
|
261
|
-
(): AsyncReducer<number, number | undefined>;
|
|
262
|
-
<O>(otherwise: AsyncOptLazy<O>): AsyncReducer<number, number | O>;
|
|
263
|
-
};
|
|
264
|
-
/**
|
|
265
|
-
* Returns a `Reducer` that remembers the maximum value of the inputs using the given `compFun` to compare input values
|
|
266
|
-
* @param compFun - a comparison function for two input values, returning 0 when equal, positive when greater, negetive when smaller
|
|
267
|
-
* @param otherwise - (default: undefined) a fallback value when there were no input values given
|
|
268
|
-
* @example
|
|
269
|
-
* ```ts
|
|
270
|
-
* const stream = Stream.of('abc', 'a', 'abcde', 'ab')
|
|
271
|
-
* console.log(stream.maxBy((s1, s2) => s1.length - s2.length))
|
|
272
|
-
* // 'abcde'
|
|
273
|
-
* ```
|
|
274
|
-
*/
|
|
275
|
-
const maxBy: {
|
|
276
|
-
<T>(compFun: (v1: T, v2: T) => MaybePromise<number>): AsyncReducer<T, T | undefined>;
|
|
277
|
-
<T, O>(compFun: (v1: T, v2: T) => MaybePromise<number>, otherwise: AsyncOptLazy<O>): AsyncReducer<T, T | O>;
|
|
278
|
-
};
|
|
279
|
-
/**
|
|
280
|
-
* Returns a `Reducer` that remembers the maximum value of the numberic inputs.
|
|
281
|
-
* @param otherwise - (default: undefined) a fallback value when there were no input values given
|
|
282
|
-
* @example
|
|
283
|
-
* ```ts
|
|
284
|
-
* console.log(Stream.of(5, 3, 7, 4).reduce(Reducer.max()))
|
|
285
|
-
* // => 7
|
|
286
|
-
* ```
|
|
287
|
-
*/
|
|
288
|
-
const max: {
|
|
289
|
-
(): AsyncReducer<number, number | undefined>;
|
|
290
|
-
<O>(otherwise: AsyncOptLazy<O>): AsyncReducer<number, number | O>;
|
|
291
|
-
};
|
|
292
|
-
/**
|
|
293
|
-
* Returns a `Reducer` that joins the given input values into a string using the given options.
|
|
294
|
-
* @param options - an object containing:<br/>
|
|
295
|
-
* - sep: (optional) a seperator string value between values in the output<br/>
|
|
296
|
-
* - start: (optional) a start string to prepend to the output<br/>
|
|
297
|
-
* - end: (optional) an end string to append to the output<br/>
|
|
298
|
-
* @example
|
|
299
|
-
* ```ts
|
|
300
|
-
* console.log(Stream.of(1, 2, 3).reduce(Reducer.join({ sep: '-' })))
|
|
301
|
-
* // => '1-2-3'
|
|
302
|
-
* ```
|
|
303
|
-
*/
|
|
304
|
-
function join<T>({ sep, start, end, valueToString, }?: {
|
|
305
|
-
sep?: string | undefined;
|
|
306
|
-
start?: string | undefined;
|
|
307
|
-
end?: string | undefined;
|
|
308
|
-
valueToString?: ((value: T) => string) | undefined;
|
|
309
|
-
}): AsyncReducer<T, string>;
|
|
310
|
-
/**
|
|
311
|
-
* Returns an `AsyncReducer` that remembers the amount of input items provided.
|
|
312
|
-
* @param pred - (optional) a predicate that returns false if the item should not be counted given:<br/>
|
|
313
|
-
* - value: the current input value<br/>
|
|
314
|
-
* - index: the input value index
|
|
315
|
-
* @example
|
|
316
|
-
* ```ts
|
|
317
|
-
* const stream = AsyncStream.from(Stream.range({ amount: 10 }))
|
|
318
|
-
* console.log(await stream.reduce(AsyncReducer.count()))
|
|
319
|
-
* // => 10
|
|
320
|
-
* console.log(await stream.reduce(AsyncReducer.count(async v => v < 5)))
|
|
321
|
-
* // => 5
|
|
322
|
-
* ```
|
|
323
|
-
*/
|
|
324
|
-
const count: {
|
|
325
|
-
(): AsyncReducer<never, number>;
|
|
326
|
-
<T>(pred: (value: T, index: number) => MaybePromise<boolean>): AsyncReducer<T, number>;
|
|
327
|
-
};
|
|
328
|
-
/**
|
|
329
|
-
* Returns an `AsyncReducer` that remembers the first input value for which the given `pred` function returns true.
|
|
330
|
-
* @param pred - a function taking an input value and its index, and returning true if the value should be remembered
|
|
331
|
-
* @param otherwise - (default: undefined) a fallback value to output if no input value yet has satisfied the given predicate
|
|
332
|
-
* @typeparam T - the input value type
|
|
333
|
-
* @typeparam O - the fallback value type
|
|
334
|
-
* @example
|
|
335
|
-
* ```ts
|
|
336
|
-
* await AsyncStream.from(Stream.range({ amount: 10 })).reduce(AsyncReducer.firstWhere(async v => v > 5)))
|
|
337
|
-
* // => 6
|
|
338
|
-
* ```
|
|
339
|
-
*/
|
|
340
|
-
const firstWhere: {
|
|
341
|
-
<T>(pred: (value: T, index: number) => MaybePromise<boolean>): AsyncReducer<T, T | undefined>;
|
|
342
|
-
<T, O>(pred: (value: T, index: number) => MaybePromise<boolean>, otherwise: AsyncOptLazy<O>): AsyncReducer<T, T | O>;
|
|
343
|
-
};
|
|
344
|
-
/**
|
|
345
|
-
* Returns an `AsyncReducer` that remembers the first input value.
|
|
346
|
-
* @param otherwise - (default: undefined) a fallback value to output if no input value has been provided
|
|
347
|
-
* @typeparam T - the input value type
|
|
348
|
-
* @typeparam O - the fallback value type
|
|
349
|
-
* @example
|
|
350
|
-
* ```ts
|
|
351
|
-
* await AsyncStream.from(Stream.range{ amount: 10 })).reduce(AsyncReducer.first())
|
|
352
|
-
* // => 0
|
|
353
|
-
* ```
|
|
354
|
-
*/
|
|
355
|
-
const first: {
|
|
356
|
-
<T>(): AsyncReducer<T, T | undefined>;
|
|
357
|
-
<T, O>(otherwise: AsyncOptLazy<O>): AsyncReducer<T, T | O>;
|
|
358
|
-
};
|
|
359
|
-
/**
|
|
360
|
-
* Returns an `AsyncReducer` that remembers the last input value for which the given `pred` function returns true.
|
|
361
|
-
* @param pred - a function taking an input value and its index, and returning true if the value should be remembered
|
|
362
|
-
* @param otherwise - (default: undefined) a fallback value to output if no input value yet has satisfied the given predicate
|
|
363
|
-
* @typeparam T - the input value type
|
|
364
|
-
* @typeparam O - the fallback value type
|
|
365
|
-
* @example
|
|
366
|
-
* ```ts
|
|
367
|
-
* await AsyncStream.from(Stream.range({ amount: 10 })).reduce(AsyncReducer.lastWhere(async v => v > 5)))
|
|
368
|
-
* // => 9
|
|
369
|
-
* ```
|
|
370
|
-
*/
|
|
371
|
-
const lastWhere: {
|
|
372
|
-
<T>(pred: (value: T, index: number) => MaybePromise<boolean>): AsyncReducer<T, T | undefined>;
|
|
373
|
-
<T, O>(pred: (value: T, index: number) => MaybePromise<boolean>, otherwise: AsyncOptLazy<O>): AsyncReducer<T, T | O>;
|
|
374
|
-
};
|
|
375
|
-
/**
|
|
376
|
-
* Returns an `AsyncReducer` that remembers the last input value.
|
|
377
|
-
* @param otherwise - (default: undefined) a fallback value to output if no input value has been provided
|
|
378
|
-
* @typeparam T - the input value type
|
|
379
|
-
* @typeparam O - the fallback value type
|
|
380
|
-
* @example
|
|
381
|
-
* ```ts
|
|
382
|
-
* await AsyncStream.from(Stream.range{ amount: 10 })).reduce(AsyncReducer.last())
|
|
383
|
-
* // => 9
|
|
384
|
-
* ```
|
|
385
|
-
*/
|
|
386
|
-
const last: {
|
|
387
|
-
<T>(): AsyncReducer<T, T | undefined>;
|
|
388
|
-
<T, O>(otherwise: AsyncOptLazy<O>): AsyncReducer<T, T | O>;
|
|
389
|
-
};
|
|
390
|
-
/**
|
|
391
|
-
* Returns a `Reducer` that ouputs false as long as no input value satisfies given `pred`, true otherwise.
|
|
392
|
-
* @typeparam T - the element type
|
|
393
|
-
* @param pred - a function taking an input value and its index, and returning true if the value satisfies the predicate
|
|
394
|
-
* @example
|
|
395
|
-
* ```ts
|
|
396
|
-
* await AsyncStream.from(Stream.range{ amount: 10 })).reduce(AsyncReducer.some(async v => v > 5))
|
|
397
|
-
* // => true
|
|
398
|
-
* ```
|
|
399
|
-
*/
|
|
400
|
-
function some<T>(pred: (value: T, index: number) => MaybePromise<boolean>): AsyncReducer<T, boolean>;
|
|
401
|
-
/**
|
|
402
|
-
* Returns an `AsyncReducer` that ouputs true as long as all input values satisfy the given `pred`, false otherwise.
|
|
403
|
-
* @typeparam T - the element type
|
|
404
|
-
* @param pred - a function taking an input value and its index, and returning true if the value satisfies the predicate
|
|
405
|
-
* @example
|
|
406
|
-
* ```ts
|
|
407
|
-
* await AsyncStream.from(Stream.range{ amount: 10 })).reduce(AsyncReducer.every(async v => v < 5))
|
|
408
|
-
* // => false
|
|
409
|
-
* ```
|
|
410
|
-
*/
|
|
411
|
-
function every<T>(pred: (value: T, index: number) => MaybePromise<boolean>): AsyncReducer<T, boolean>;
|
|
412
|
-
/**
|
|
413
|
-
* Returns an `AsyncReducer` that outputs false as long as the given `elem` has not been encountered in the input values, true otherwise.
|
|
414
|
-
* @typeparam T - the element type
|
|
415
|
-
* @param elem - the element to search for
|
|
416
|
-
* @param eq - (optional) a comparison function that returns true if te two given input values are considered equal
|
|
417
|
-
* @example
|
|
418
|
-
* ```ts
|
|
419
|
-
* await AsyncStream.from(Stream.range({ amount: 10 })).reduce(AsyncReducer.contains(5)))
|
|
420
|
-
* // => true
|
|
421
|
-
* ```
|
|
422
|
-
*/
|
|
423
|
-
function contains<T>(elem: T, eq?: Eq<T>): AsyncReducer<T, boolean>;
|
|
424
|
-
/**
|
|
425
|
-
* Returns an `AsyncReducer` that takes boolean values and outputs true if all input values are true, and false otherwise.
|
|
426
|
-
* @example
|
|
427
|
-
* ```ts
|
|
428
|
-
* await AsyncStream.of(true, false, true)).reduce(AsyncReducer.and))
|
|
429
|
-
* // => false
|
|
430
|
-
* ```
|
|
431
|
-
*/
|
|
432
|
-
const and: AsyncReducer<boolean, boolean>;
|
|
433
|
-
/**
|
|
434
|
-
* Returns an `AsyncReducer` that takes boolean values and outputs true if one or more input values are true, and false otherwise.
|
|
435
|
-
* @example
|
|
436
|
-
* ```ts
|
|
437
|
-
* await AsyncStream.of(true, false, true)).reduce(AsyncReducer.or))
|
|
438
|
-
* // => true
|
|
439
|
-
* ```
|
|
440
|
-
*/
|
|
441
|
-
const or: AsyncReducer<boolean, boolean>;
|
|
442
|
-
/**
|
|
443
|
-
* Returns an `AsyncReducer` that outputs true if no input values are received, false otherwise.
|
|
444
|
-
* @example
|
|
445
|
-
* ```ts
|
|
446
|
-
* await AsyncStream.of(1, 2, 3).reduce(AsyncReducer.isEmpty))
|
|
447
|
-
* // => false
|
|
448
|
-
* ```
|
|
449
|
-
*/
|
|
450
|
-
const isEmpty: AsyncReducer<never, boolean>;
|
|
451
|
-
/**
|
|
452
|
-
* Returns an `AsyncReducer` that outputs true if one or more input values are received, false otherwise.
|
|
453
|
-
* @example
|
|
454
|
-
* ```ts
|
|
455
|
-
* await AsyncStream.of(1, 2, 3).reduce(AsyncReducer.nonEmpty))
|
|
456
|
-
* // => true
|
|
457
|
-
* ```
|
|
458
|
-
*/
|
|
459
|
-
const nonEmpty: AsyncReducer<never, boolean>;
|
|
460
|
-
/**
|
|
461
|
-
* Returns an `AsyncReducer` that collects received input values in an array, and returns a copy of that array as an output value when requested.
|
|
462
|
-
* @typeparam T - the element type
|
|
463
|
-
* @example
|
|
464
|
-
* ```ts
|
|
465
|
-
* await AsyncStream.of(1, 2, 3).reduce(AsyncReducer.toArray()))
|
|
466
|
-
* // => [1, 2, 3]
|
|
467
|
-
* ```
|
|
468
|
-
*/
|
|
469
|
-
function toArray<T>(): AsyncReducer<T, T[]>;
|
|
470
|
-
/**
|
|
471
|
-
* Returns a `AsyncReducer` that collects received input tuples into a mutable JS Map, and returns
|
|
472
|
-
* a copy of that map when output is requested.
|
|
473
|
-
* @typeparam K - the map key type
|
|
474
|
-
* @typeparam V - the map value type
|
|
475
|
-
* @example
|
|
476
|
-
* ```ts
|
|
477
|
-
* await AsyncStream.of([1, 'a'], [2, 'b']).reduce(AsyncReducer.toJSMap()))
|
|
478
|
-
* // Map { 1 => 'a', 2 => 'b' }
|
|
479
|
-
* ```
|
|
480
|
-
*/
|
|
481
|
-
function toJSMap<K, V>(): AsyncReducer<[K, V], Map<K, V>>;
|
|
482
|
-
/**
|
|
483
|
-
* Returns an `AsyncReducer` that collects received input values into a mutable JS Set, and returns
|
|
484
|
-
* a copy of that map when output is requested.
|
|
485
|
-
* @typeparam T - the element type
|
|
486
|
-
* @example
|
|
487
|
-
* ```ts
|
|
488
|
-
* await AsyncStream.of(1, 2, 3).reduce(AsyncReducer.toJSSet()))
|
|
489
|
-
* // Set {1, 2, 3}
|
|
490
|
-
* ```
|
|
491
|
-
*/
|
|
492
|
-
function toJSSet<T>(): AsyncReducer<T, Set<T>>;
|
|
493
|
-
/**
|
|
494
|
-
* Returns an `AsyncReducer` that collects 2-tuples containing keys and values into a plain JS object, and
|
|
495
|
-
* returns a copy of that object when output is requested.
|
|
496
|
-
* @typeparam K - the result object key type
|
|
497
|
-
* @typeparam V - the result object value type
|
|
498
|
-
* @example
|
|
499
|
-
* ```ts
|
|
500
|
-
* await AsyncStream.of(['a', 1], ['b', true]).reduce(AsyncReducer.toJSObject()))
|
|
501
|
-
* // { a: 1, b: true }
|
|
502
|
-
* ```
|
|
503
|
-
*/
|
|
504
|
-
function toJSObject<K extends string | number | symbol, V>(): AsyncReducer<[K, V], Record<K, V>>;
|
|
505
|
-
/**
|
|
506
|
-
* Returns an `AsyncReducer` that combines multiple input `reducers` by providing input values to all of them and collecting the outputs in an array.
|
|
507
|
-
* @param reducers - 2 or more reducers to combine
|
|
508
|
-
* @example
|
|
509
|
-
* ```ts
|
|
510
|
-
* const red = AsyncReducer.combineArr(AsyncReducer.sum, AsyncReducer.average)
|
|
511
|
-
*
|
|
512
|
-
* await AsyncStream.from(Stream.range({ amount: 9 }))
|
|
513
|
-
* .reduce(red)
|
|
514
|
-
* // => [36, 4]
|
|
515
|
-
* ```
|
|
516
|
-
*/
|
|
517
|
-
function combineArr<T, R extends readonly [unknown, unknown, ...unknown[]]>(...reducers: {
|
|
518
|
-
[K in keyof R]: AsyncReducer<T, R[K]>;
|
|
519
|
-
} & AsyncReducer<T, unknown>[]): AsyncReducer<T, R>;
|
|
520
|
-
/**
|
|
521
|
-
* Returns an `AsyncReducer` that combines multiple input `reducers` by providing input values to all of them and collecting the outputs in the shape of the given object.
|
|
522
|
-
* @typeparam T - the input type for all the reducers
|
|
523
|
-
* @typeparam R - the result object shape
|
|
524
|
-
* @param reducerObj - an object of keys, and reducers corresponding to those keys
|
|
525
|
-
* @example
|
|
526
|
-
* ```ts
|
|
527
|
-
* const red = AsyncReducer.combineObj({
|
|
528
|
-
* theSum: Reducer.sum,
|
|
529
|
-
* theAverage: Reducer.average
|
|
530
|
-
* });
|
|
531
|
-
*
|
|
532
|
-
* await AsyncStream.from(Stream.range({ amount: 9 }))
|
|
533
|
-
* .reduce(red));
|
|
534
|
-
* // => { theSum: 36, theAverage: 4 }
|
|
535
|
-
* ```
|
|
536
|
-
*/
|
|
537
|
-
function combineObj<T, R extends {
|
|
538
|
-
readonly [key: string]: unknown;
|
|
539
|
-
}>(reducerObj: {
|
|
540
|
-
readonly [K in keyof R]: AsyncReducer<T, R[K]>;
|
|
541
|
-
} & Record<string, AsyncReducer<T, unknown>>): AsyncReducer<T, R>;
|
|
542
|
-
}
|