-
Notifications
You must be signed in to change notification settings - Fork 0
/
session1.ts
282 lines (217 loc) · 5.81 KB
/
session1.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
interface IteratorYieldResult<TYield> {
done?: false;
value: TYield;
}
interface IteratorReturnResult {
done: true;
value: undefined;
}
interface Iterator<T> {
next(): IteratorYieldResult<T> | IteratorReturnResult;
}
// function naturals(end = Infinity): IterableIterator<number> {
// let i = 0;
// return {
// next() {
// return ++i <= end
// ? { value: i, done: false }
// : { value: undefined, done: true }
// },
// [Symbol.iterator]() { return this; }
// }
// }
function* naturals(end = Infinity) {
let i = 0;
while (++i <= end) {
yield i;
}
}
function reverse<T>(arr: T[]): IterableIterator<T> {
let { length } = arr;
return {
next() {
return length
? { value: arr[--length], done: false }
: { value: undefined, done: true }
},
[Symbol.iterator]() { return this; }
}
}
// function logEach2<T>(iterable: Iterable<T>) {
// const iterator = iterable[Symbol.iterator]();
// while (true) {
// const { value, done } = iterator.next();
// if (done) break;
// console.log(value);
// }
// }
function logEach<T>(iterable: Iterable<T>) {
for (const value of iterable) {
console.log(value);
}
}
export function main() {
// window.naturals = naturals;
// arr[0]
const arr = ['A', 'B', 'C', 'D'];
console.log(arr);
// console.log(arr.length);
// console.log(arr[1]);
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
console.log('----');
// arr.at(-1);
for (let i = -1; i >= -arr.length; i--) {
console.log(-i, arr.at(i));
}
console.log('----');
// Set
const set = new Set([2, 2, 4, 6, 6]);
set.forEach(a => console.log(a));
// function naturals(): Iterator<number> {};
const iterator = naturals(3);
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
const iterator2 = naturals(2);
while (true) {
const { value, done } = iterator2.next();
if (done) break;
console.log(value);
}
console.clear();
// logEach
logEach(naturals(3));
// arr.values()
// const iterator3 = arr.values();
// console.log(iterator3.next());
// console.log(iterator3.next());
// console.log(iterator3.next());
// console.log(iterator3.next());
// console.log(iterator3.next());
logEach(arr.values());
// set.values()
logEach(set.values());
// reverse()
console.log(arr);
// const arr2 = [...arr].reverse();
// arr2[1]
// arr2[2]
logEach(reverse(arr));
console.clear();
// IterableIterator
for (const value of arr) {
console.log(value);
}
for (const value of naturals(3)) {
console.log(value);
}
for (const value of reverse(arr)) {
console.log(value);
}
console.log([...reverse(arr)]);
console.log([...arr]);
console.clear();
// Generator
console.log([...naturals(3)]);
logEach(naturals(5));
// upperCaseAll
function* upperCaseAllLazy(iterable: Iterable<string>) {
for (const value of iterable) {
yield value.toUpperCase();
}
}
function upperCaseAll(iterable: Iterable<string>) {
return [...upperCaseAllLazy(iterable)];
}
// const iterator5 = upperCaseAllLazy(['a', 'b', 'c', 'd']);
// console.log(iterator5.next());
// console.log(iterator5.next());
const [first] = upperCaseAllLazy(['a', 'b', 'c', 'd', '...']);
console.log(first);
console.log(upperCaseAll(['a', 'b']));
console.log(...map(a => a.toUpperCase(), ['a', 'b']));
// console.log(upperCaseAll(new Set(['a', 'a', 'b', 'c'])));
// doubleAll
// function* doubleAllLazy(iterable: Iterable<number>) {
// for (const value of iterable) {
// yield value * 2;
// }
// }
function doubleAllLazy(iterable: Iterable<number>) {
return map(a => a * 2, iterable);
}
function doubleAll(iterable: Iterable<number>) {
return [...doubleAllLazy(iterable)];
}
const iterator6 = doubleAllLazy([1, 2, 3, 4]);
logEach(iterator6);
console.log(doubleAll([10, 20, 30]));
// filterOdds
function* filterOddsLazy(iterable: Iterable<number>) {
for (const value of iterable) {
if (value % 2 === 1) {
yield value;
}
}
}
console.log([...filterOddsLazy([1, 2, 3, 4, 5, 6])]);
// filterSingleChars
function* filterSingleChars(iterable: Iterable<string>) {
for (const value of iterable) {
if (value.length === 1) {
yield value;
}
}
}
console.log(...filterSingleChars(['aa', 'a', 'b', 'cc', 'd']));
const iterator7 = filterSingleChars(['aa', 'a', 'b', 'cc', 'd']);
console.log(iterator7.next());
console.log(iterator7.next());
console.log(iterator7.next());
console.log(iterator7.next());
// ----- { next() {} }
// + 일급함수
// map
function* map<A, B>(f: (a: A) => B, iterable: Iterable<A>): IterableIterator<B> {
for (const value of iterable) {
yield f(value);
}
}
// function* filterOddsLazy(iterable: Iterable<number>) {
// for (const value of iterable) {
// if (value % 2 === 1) {
// yield value;
// }
// }
// }
// filter
function* filter<A>(f: (a: A) => boolean, iterable: Iterable<A>): IterableIterator<A> {
for (const value of iterable) {
if (f(value)) {
yield value;
}
}
}
function forEach<A>(f: (a: A) => void, iterable: Iterable<A>) {
for (const value of iterable) {
f(value);
}
}
const isOdd = (a: number) => a % 2 === 1;
console.clear();
logEach(filterOddsLazy([1, 2, 3, 4, 5]));
logEach(filter(isOdd, [1, 2, 3, 4, 5]));
// forEach
forEach(console.log, filter(isOdd, [1, 2, 3, 4, 5]));
console.clear();
// NodeList
const divs= document.querySelectorAll('div');
forEach(console.log,
filter(isOdd,
map(text => parseInt(text),
map(node => node.textContent!,
document.querySelectorAll('section div')))));
}