-
Notifications
You must be signed in to change notification settings - Fork 3
/
functional.ts
263 lines (235 loc) · 7.7 KB
/
functional.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
export * as default from "./functional.js";
/**
* Predicate function which returns a loose boolean value
* @internal
*/
export type Predicate<Type> = (value: Type) => Maybe<AnyObject | true>;
/**
* Predicate function which attests a given a type
* @internal
*/
export type PredicateAs<Type, As extends Type = Type> = (value: Type) => value is As;
/**
* Returns a new predicate which expresses if *any* of the given predicates are true.
* @internal
*/
export function somePredicate<Type>(predicates: Iterable<Predicate<Type>>): Predicate<Type>;
/** @internal */
export function somePredicate<Type, As extends Type = Type>(predicates: Iterable<PredicateAs<Type, As>>): PredicateAs<Type, As>;
export function somePredicate(predicates: Iterable<Predicate<unknown> | PredicateAs<unknown>>) {
const { head, rest } = shift(predicates);
return reduce(
rest,
head ?? (() => false),
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
(predicate, next) => value => predicate(value) || next(value));
}
/**
* Iterate each item from an iterable of iterables.
* @internal
*/
// TypeScript can't figure out the types on these so extra hints are needed.
export function concat<Type>(iterator: Iterable<Type>[]): IterableIterator<Type>;
/** @internal */
// eslint-disable-next-line @typescript-eslint/unified-signatures
export function concat<Type>(iterator: IterableIterator<Iterable<Type>>): IterableIterator<Type>;
/** @internal */
// eslint-disable-next-line @typescript-eslint/unified-signatures
export function concat<Type>(iterator: Iterable<Iterable<Type>>): IterableIterator<Type>;
/**
* Iterate each item in a statically supplied argument vector of iterables.
* @internal
*/
export function concat<Type>(...args: Iterable<Type>[]): IterableIterator<Type>;
export function *concat(...args: any[]) {
for (const iterable of args.length === 1 ? args[0] : args) {
for (const value of iterable) {
yield value;
}
}
}
/**
* Remove all non-truthy elements from a type
* @internal
*/
export type Truthy<Type> = Type extends Maybe<void> ? never : Type;
const truthy: <Type>(value: Type) => value is Truthy<Type> = Boolean as never;
/**
* Iterates the iterable, and emits only truthy elements.
* @internal
*/
export function filter<Type>(iterable: Iterable<Type>): IterableIterator<Truthy<Type>>;
/**
* Iterates the iterable, emitting only elements which pass the predicate. You can use the type
* guard to affect the type of the resulting iterator.
* @internal
*/
export function filter<Type, Filter extends Type>(
iterable: Iterable<Type>, predicate: (value: Type) => value is Filter
): IterableIterator<Filter>;
/**
* Iterates the iterable, emitting only elements which pass the predicate.
* @internal
*/
export function filter<Type>(
iterable: Iterable<Type>, predicate: Predicate<Type>
): IterableIterator<Type>;
export function *filter(iterable: Iterable<unknown>, predicate: Predicate<unknown> = truthy) {
for (const value of iterable) {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (predicate(value)) {
yield value;
}
}
}
/**
* Intersperses `separator` between every element of an iterable.
*/
export function *intersperse<Type, Separator>(iterable: Iterable<Type>, separator: Separator): IterableIterator<Type | Separator> {
let first = true;
for (const value of iterable) {
if (first) {
first = false;
} else {
yield separator;
}
yield value;
}
}
/**
* Eagerly folds the iterable, joining the results with the given separator.
*/
export function join(iterable: Iterable<string>, separator = "") {
return reduce(intersperse(iterable, separator), "", (accumulator, fragment) => `${accumulator}${fragment}`);
}
/**
* Applies a given function to an iterable.
* @internal
*/
export function *map<Type, Result>(
iterable: Iterable<Type>,
callback: (value: Type) => Result,
): IterableIterator<Result> {
for (const value of iterable) {
yield callback(value);
}
}
/**
* Applies a given async function to an iterable, and returns a promise to an array of the results.
* @internal
*/
export function mapAwait<Type, Result>(
iterable: Iterable<Type>,
callback: (value: Type) => Result | PromiseLike<Result>,
): Promise<Result[]> {
return Promise.all(map(iterable, callback));
}
/**
* Eagerly iterates the given iterable, invoking the reducer for each element in the iterable.
* Uses the previous result as the first parameter and the current value as the second.
* @internal
*/
export function reduce<Type, Result = Type>(
iterable: Iterable<Type>,
initial: Result,
reducer: (accumulator: Result, value: Type) => Result,
) {
let result = initial;
for (const value of iterable) {
result = reducer(result, value);
}
return result;
}
/**
* Iterates an array in reverse without modifying the original array.
* @internal
*/
export function *reverse<Type>(array: readonly Type[]): IterableIterator<Type> {
for (let ii = array.length - 1; ii >= 0; --ii) {
yield array[ii]!;
}
}
/**
* Returns the first element from an iterable, as well as another iterable that will continue after
* the shifted element.
* @internal
*/
export function shift<Type>(iterable: Iterable<Type>) {
const iterator = iterable[Symbol.iterator]();
const { done, value } = iterator.next();
const rest: Iterable<Type> = {
[Symbol.iterator]() {
return iterator;
},
};
return {
head: done ? undefined : value as Type,
rest,
};
}
/**
* Returns `true` if the predicate is truthy for any element, otherwise `false`. Eagerly iterates
* the whole iterable until a truthy value is found.
* @internal
*/
export function some(iterable: Iterable<Maybe<boolean | AnyObject>>): boolean;
/** @internal */
export function some<Type>(iterable: Iterable<Type>, predicate: (value: Type) => Maybe<boolean | AnyObject>): boolean;
export function some(iterable: Iterable<unknown>, predicate = (value: unknown) => value) {
for (const value of iterable) {
if (predicate(value)) {
return true;
}
}
return false;
}
/**
* Similar to `map` except the mapper returns an iterable which delegates to the result.
* @internal
*/
export function *transform<Type, Result>(
iterable: Iterable<Type>,
callback: (value: Type) => Iterable<Result>,
): IterableIterator<Result> {
for (const value of iterable) {
yield* callback(value);
}
}
/**
* Comparator for two values. If the values are equal it must return 0, if `left` is less than
* `right` then it must return a value less than 0, and otherwise it returns a value greater than 0.
* @internal
*/
export type Comparator<Type> = (left: Type, right: Type) => number;
type PrimitiveComparable = bigint | boolean | string;
/**
* A comparator which can be used mainly for strings, but also bigint / booleans if you feel the
* need for that kind of thing. You could use it for numbers too, but that's better suited to
* `numericComparator` so the types don't permit it in that case.
* @internal
*/
export function primitiveComparator<Type extends PrimitiveComparable>(left: Type, right: Type) {
return left < right ? -1 : left === right ? 0 : 1;
}
/**
* Comparator for numeric types.
* @internal
*/
export function numericComparator(left: number, right: number) {
return left - right;
}
/**
* Creates a comparator from a mapping function and a comparator.
* @internal
*/
export function mappedComparator<Type, Result>(comparator: Comparator<Result>, map: (value: Type) => Result): Comparator<Type> {
return (left, right) => comparator(map(left), map(right));
}
/** @internal */
export function mappedNumericComparator<Type>(map: (value: Type) => number) {
return mappedComparator(numericComparator, map);
}
/** @internal */
export function mappedPrimitiveComparator<Type>(map: (value: Type) => PrimitiveComparable) {
return mappedComparator(primitiveComparator, map);
}