-
Notifications
You must be signed in to change notification settings - Fork 64
/
scan.ts
202 lines (179 loc) · 5.24 KB
/
scan.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
import { empty, isAsyncIterable, isIterable } from "../_internal/utils";
import head from "../head";
import pipe1 from "../pipe1";
import type Arrow from "../types/Arrow";
import type Awaited from "../types/Awaited";
import type IterableInfer from "../types/IterableInfer";
import type ReturnIterableIteratorType from "../types/ReturnIterableIteratorType";
import concurrent, { isConcurrent } from "./concurrent";
function* sync<A, B>(
f: (a: B, b: A) => B,
acc: B,
iterable: Iterable<A>,
): IterableIterator<B> {
yield acc;
for (const a of iterable) {
yield (acc = f(acc, a));
}
}
async function* asyncSequential<A, B>(
f: (a: B, b: A) => B | Promise<B>,
acc: B | Promise<B>,
iterable: AsyncIterable<A>,
): AsyncIterableIterator<B> {
yield acc;
for await (const a of iterable) {
yield (acc = pipe1(acc, (acc) => f(acc, a)) as B | Promise<B>);
}
}
function async<A, B>(
f: (a: B, b: A) => B | Promise<B>,
acc: B | Promise<B>,
iterable: AsyncIterable<A>,
): AsyncIterableIterator<B> {
let _iterator: AsyncIterator<B>;
return {
async next(_concurrent: any) {
if (_iterator === undefined) {
_iterator = isConcurrent(_concurrent)
? asyncSequential(f, acc, concurrent(_concurrent.length, iterable))
: asyncSequential(f, acc, iterable);
}
return _iterator.next(_concurrent);
},
[Symbol.asyncIterator]() {
return this;
},
};
}
function asyncWithoutSeed<A, B>(
f: (a: B, b: A) => B,
iterable: AsyncIterable<A>,
): AsyncIterableIterator<B> {
let _iterator: AsyncIterator<B>;
return {
async next(_concurrent: any) {
if (_iterator === undefined) {
if (isConcurrent(_concurrent)) {
const _iterable = concurrent(_concurrent.length, iterable);
_iterator = asyncSequential(f, head(_iterable) as any, _iterable);
} else {
_iterator = asyncSequential(f, head(iterable) as any, iterable);
}
}
return _iterator.next(_concurrent);
},
[Symbol.asyncIterator]() {
return this;
},
};
}
/**
* Returns a Iterable/AsyncIterable of successively reduced values from the left.
* It's similar to reduce
*
* @example
* ```ts
* const iter = scan((acc, cur) => acc * cur, 1, [1, 2, 3, 4 ]);
* iter.next(); // {value: 1, done:false}
* iter.next(); // {value: 1, done:false}
* iter.next(); // {value: 2, done:false}
* iter.next(); // {value: 6, done:false}
* iter.next(); // {value: 24, done:false}
* iter.next(); // {value: undefined, done: true}
*
* // with pipe
* pipe(
* [1, 2, 3, 4],
* scan((acc, cur) => acc * cur),
* toArray,
* ); // [1, 2, 6, 24]
* ```
*/
function scan<A extends readonly []>(
f: Arrow,
iterable: A,
): IterableIterator<never>;
function scan<A extends readonly [], B>(
f: Arrow,
seed: B,
iterable: A,
): IterableIterator<B>;
function scan<A>(
f: (a: A, b: A) => A,
iterable: Iterable<A>,
): IterableIterator<A>;
function scan<A, B>(
f: (a: B, b: A) => B,
seed: B,
iterable: Iterable<A>,
): IterableIterator<B>;
function scan<A, B>(
f: (a: B, b: A) => B,
iterable: Iterable<A>,
): IterableIterator<B>;
function scan<A>(
f: (a: A, b: A) => A | Promise<A>,
iterable: AsyncIterable<A>,
): AsyncIterableIterator<A>;
function scan<A, B>(
f: (a: Awaited<B>, b: A) => B | Promise<B>,
seed: B | Promise<B>,
iterable: AsyncIterable<A>,
): AsyncIterableIterator<B>;
function scan<A, B>(
f: (a: Awaited<B>, b: A) => B | Promise<B>,
iterable: AsyncIterable<A>,
): AsyncIterableIterator<Awaited<B>>;
function scan<A extends Iterable<unknown> | AsyncIterable<unknown>>(
f: (
a: IterableInfer<A>,
b: IterableInfer<A>,
) => IterableInfer<A> | Promise<IterableInfer<A>>,
): (iterable: A) => ReturnIterableIteratorType<A, IterableInfer<A>>;
function scan<A extends Iterable<unknown> | AsyncIterable<unknown>, B>(
f: (a: B, b: IterableInfer<A>) => B | Promise<B>,
): (iterable: A) => ReturnIterableIteratorType<A, B>;
function scan<A extends Iterable<unknown> | AsyncIterable<unknown>, B>(
f: (a: B, b: IterableInfer<A>) => B,
seed?: B | Iterable<IterableInfer<A>> | AsyncIterable<IterableInfer<A>>,
iterable?: Iterable<IterableInfer<A>> | AsyncIterable<IterableInfer<A>>,
):
| IterableIterator<B>
| AsyncIterableIterator<B>
| ((iterable: A) => ReturnIterableIteratorType<A, B>) {
if (iterable === undefined) {
if (seed === undefined) {
return (iterable: A) => {
return scan(
f,
iterable as any,
) as unknown as ReturnIterableIteratorType<A, B>;
};
}
if (isIterable(seed)) {
const iterator = seed[Symbol.iterator]();
const { done, value } = iterator.next();
if (done) {
return empty() as IterableIterator<B>;
}
return sync(f, value as B, {
[Symbol.iterator]() {
return iterator;
},
});
}
if (isAsyncIterable(seed)) {
return asyncWithoutSeed(f, seed);
}
throw new TypeError("'iterable' must be type of Iterable or AsyncIterable");
}
if (isIterable(iterable)) {
return sync(f, seed as B, iterable);
}
if (isAsyncIterable(iterable)) {
return async(f, Promise.resolve(seed as B), iterable);
}
throw new TypeError("'iterable' must be type of Iterable or AsyncIterable");
}
export default scan;