-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.ts
148 lines (128 loc) · 3.14 KB
/
sync.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
export class F<T> implements Iterable<T> {
[Symbol.iterator] = () => this.iterator[Symbol.iterator]();
constructor(private readonly iterator: Iterable<T>) {
}
all(fn: (item: T) => boolean): boolean {
for(const item of this.iterator)
if(!fn(item)) return false;
return true;
}
concat<U>(other: Iterable<U>): F<T | U>
concat<U>(...other: U[]): F<T | U>
concat<U>(other: Iterable<U>): F<T | U> {
if(!other[Symbol.iterator]) other = arguments;
return new F((function* (items) {
yield* items;
yield* other;
})(this.iterator));
}
count(fn?: (item: T) => boolean): number {
let count = 0;
for(const item of this.iterator)
if(!fn || fn(item)) ++count;
return count;
}
filter<U extends T>(fn?: (item: T) => item is U): F<U>
filter(fn?: (item: T) => boolean): F<T>
filter(fn?: (item: T) => boolean): F<T> {
if(!fn) fn = (x) => !!x;
return new F((function* (items) {
for(const item of items) {
if(fn(item))
yield item;
}
})(this.iterator));
}
first(fn?: (item: T) => boolean): T | undefined {
for(const item of this.iterator)
if(!fn || fn(item))
return item;
}
flatMap<U>(fn: (item: T) => Iterable<U> | undefined): F<U> {
return new F((function* (items) {
for(const item of items) {
const value = fn(item);
if(value)
yield* value;
}
})(this.iterator));
}
includes(item: T): boolean {
return this.some(x => x === item);
}
map<U>(fn: (item: T) => U): F<U> {
return new F((function* (items) {
for(const item of items)
yield fn(item);
})(this.iterator));
}
reduce<U>(fn: (current: U, item: T) => U, current: U): U {
for(const item of this.iterator)
current = fn(current, item);
return current;
}
skip(count: number): F<T> {
return new F((function* (items) {
for(const item of items)
if(count-- <= 0)
yield item;
})(this.iterator));
}
some(fn: (item: T) => boolean): boolean {
for(const item of this.iterator)
if(fn(item)) return true;
return false;
}
take(count: number): F<T> {
return new F((function* (items) {
for(const item of items) {
if(count-- <= 0) return;
yield item;
}
})(this.iterator));
}
toArray(): T[] {
return [...this.iterator];
}
toMap<U>(key: (item: T) => U): Map<U, T> {
const map = new Map<U, T>();
for(const item of this.iterator)
map.set(key(item), item);
return map;
}
toSet(): Set<T> {
return new Set(this.iterator);
}
unique<U>(fn?: (item: T) => Promise<U> | U): F<T> {
const set = new Set();
return new F((function* (items) {
for(const item of items) {
const key = fn ? fn(item) : item;
if(!set.has(key)) {
yield item;
set.add(key);
}
}
})(this.iterator));
}
}
export default function f<T>(iterable: Iterable<T>) {
return new F(iterable);
}
f.range = function(from: number, to: number): F<number> {
++to;
const sign = Math.sign(to - from);
return new F((function*() {
for(; from !== to; from += sign)
yield from;
})());
}
f.while = <T>(fn: () => T): F<NonNullable<T>> => {
let value = fn();
return new F<NonNullable<T>>((function*() {
while(value !== undefined && value !== null) {
yield value as NonNullable<T>;
value = fn();
}
})());
}