forked from baetheus/fun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_iterable.ts
332 lines (303 loc) · 7.51 KB
/
async_iterable.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import type { Filterable } from "./filterable.ts";
import type { Option } from "./option.ts";
import type { Kind, Out } from "./kind.ts";
import type { Monad } from "./monad.ts";
import type { Either } from "./either.ts";
import type { Pair } from "./pair.ts";
import type { Predicate } from "./predicate.ts";
import type { Refinement } from "./refinement.ts";
import { isLeft, isRight } from "./either.ts";
import { isSome } from "./option.ts";
import { wait } from "./promise.ts";
export interface KindAsyncIterable extends Kind {
readonly kind: AsyncIterable<Out<this, 0>>;
}
const isAsyncIterator = <A>(
o: (() => AsyncIterator<A>) | AsyncGenerator<A>,
): o is AsyncGenerator<A> => Object.hasOwn(o, Symbol.asyncIterator);
export function make<A>(
fa: (() => AsyncIterator<A>) | AsyncGenerator<A>,
): AsyncIterable<A> {
if (isAsyncIterator(fa)) {
return fa;
}
return { [Symbol.asyncIterator]: fa };
}
export function fromIterable<A>(ta: Iterable<A>): AsyncIterable<A> {
return make(async function* () {
for (const a of ta) {
yield a;
}
});
}
export function clone<A>(ta: AsyncIterable<A>): AsyncIterable<A> {
const cache: Promise<IteratorResult<A>>[] = [];
let iterator: AsyncIterator<A>;
return make(async function* () {
if (iterator === undefined) {
iterator = ta[Symbol.asyncIterator]();
}
let index = 0;
while (true) {
if (index === cache.length) {
cache.push(iterator.next());
} else {
const { value, done } = await cache[index++];
if (done) {
break;
}
yield value;
}
}
});
}
export function of<A>(...a: A[]): AsyncIterable<A> {
return make(async function* () {
let index = -1;
const length = a.length;
while (++index < length) {
yield a[index];
}
});
}
export function ap<A>(
ua: AsyncIterable<A>,
): <I>(ufai: AsyncIterable<(a: A) => I>) => AsyncIterable<I> {
return (ufai) =>
make(async function* () {
for await (const fai of ufai) {
for await (const a of ua) {
yield fai(a);
}
}
});
}
export function map<A, I>(
fai: (a: A) => I | PromiseLike<I>,
): (ta: AsyncIterable<A>) => AsyncIterable<I> {
return (ta) =>
make(async function* () {
for await (const a of ta) {
yield await fai(a);
}
});
}
export function join<A>(
tta: AsyncIterable<AsyncIterable<A>>,
): AsyncIterable<A> {
return make(async function* () {
for await (const ta of tta) {
for await (const a of ta) {
yield a;
}
}
});
}
export function chain<A, I>(
fati: (a: A) => AsyncIterable<I>,
): (ta: AsyncIterable<A>) => AsyncIterable<I> {
return (ta) =>
make(async function* () {
for await (const a of ta) {
for await (const i of fati(a)) {
yield i;
}
}
});
}
export function forEach<A>(
onValue: (a: A) => void | PromiseLike<void>,
onDone: () => void | PromiseLike<void> = () => {},
): (ta: AsyncIterable<A>) => PromiseLike<void> {
return async (ta) => {
for await (const a of ta) {
await onValue(a);
}
onDone();
};
}
export function delay(
ms: number,
): <A>(ta: AsyncIterable<A>) => AsyncIterable<A> {
return map((a) => wait(ms).then(() => a));
}
export function filter<A, B extends A>(
refinement: Refinement<A, B>,
): (ua: AsyncIterable<A>) => AsyncIterable<B>;
export function filter<A>(
predicate: Predicate<A>,
): (ua: AsyncIterable<A>) => AsyncIterable<A>;
export function filter<A>(
predicate: Predicate<A>,
): (ta: AsyncIterable<A>) => AsyncIterable<A> {
return (ta) =>
make(async function* filter() {
for await (const a of ta) {
if (predicate(a)) {
yield a;
}
}
});
}
export function filterMap<A, I>(
predicate: (a: A) => Option<I>,
): (ua: AsyncIterable<A>) => AsyncIterable<I> {
return (ua) =>
make(
async function* filterMap() {
for await (const a of ua) {
const result = predicate(a);
if (isSome(result)) {
yield result.value;
}
}
},
);
}
export function partition<A, B extends A>(
refinement: (a: A) => a is B,
): (ua: AsyncIterable<A>) => Pair<AsyncIterable<A>, AsyncIterable<B>>;
export function partition<A>(
predicate: (a: A) => boolean,
): (ua: AsyncIterable<A>) => Pair<AsyncIterable<A>, AsyncIterable<A>>;
export function partition<A>(
predicate: (a: A) => boolean,
): (ua: AsyncIterable<A>) => Pair<AsyncIterable<A>, AsyncIterable<A>> {
return (ua) => {
const cloned = clone(ua);
return [
make(async function* partitionFirst() {
for await (const a of cloned) {
if (predicate(a)) {
yield a;
}
}
}),
make(async function* partitionFirst() {
for await (const a of cloned) {
if (!predicate(a)) {
yield a;
}
}
}),
];
};
}
export function partitionMap<A, I, J>(
predicate: (a: A) => Either<J, I>,
): (ua: AsyncIterable<A>) => Pair<AsyncIterable<I>, AsyncIterable<J>> {
return (ua) => {
const cloned = clone(ua);
return [
make(async function* partitionFirst() {
for await (const a of cloned) {
const result = predicate(a);
if (isRight(result)) {
yield result.right;
}
}
}),
make(async function* partitionSecond() {
for await (const a of cloned) {
const result = predicate(a);
if (isLeft(result)) {
yield result.left;
}
}
}),
];
};
}
export function reduce<A, O>(
foao: (o: O, a: A, i: number) => O,
o: O,
): (ta: AsyncIterable<A>) => Promise<O> {
return async (ta) => {
let index = 0;
let result = o;
for await (const a of ta) {
result = foao(result, a, index++);
}
return result;
};
}
export function takeUntil<A>(
predicate: Predicate<A>,
): (ta: AsyncIterable<A>) => AsyncIterable<A> {
return (ta) =>
make(async function* () {
for await (const a of ta) {
if (predicate(a)) {
return;
}
yield a;
}
});
}
export function takeWhile<A>(
predicate: Predicate<A>,
): (ta: AsyncIterable<A>) => AsyncIterable<A> {
return takeUntil((a) => !predicate(a));
}
export function scan<A, O>(
foao: (o: O, a: A) => O,
o: O,
): (ta: AsyncIterable<A>) => AsyncIterable<O> {
return (ta) =>
make(async function* () {
let result = o;
for await (const a of ta) {
result = foao(result, a);
yield result;
}
});
}
export function tap<A>(
fa: (a: A) => void,
): (ta: AsyncIterable<A>) => AsyncIterable<A> {
return (ta) =>
make(async function* () {
for await (const a of ta) {
fa(a);
yield a;
}
});
}
export function repeat(
n: number,
): <A>(ta: AsyncIterable<A>) => AsyncIterable<A> {
return <A>(ta: AsyncIterable<A>) =>
make(async function* () {
let index = n;
while (index-- > 0) {
for await (const a of ta) {
yield a;
}
}
});
}
export function take(n: number): <A>(ta: AsyncIterable<A>) => AsyncIterable<A> {
return <A>(ta: AsyncIterable<A>) =>
make(async function* () {
let count = Math.floor(n);
for await (const a of ta) {
if (count-- <= 0) {
return;
}
yield a;
}
});
}
export const MonadAsyncIterable: Monad<KindAsyncIterable> = {
of,
ap,
map,
join,
chain,
};
export const FilterableAsyncIterable: Filterable<KindAsyncIterable> = {
filter,
filterMap,
partition,
partitionMap,
};