Skip to content

Commit 74f024d

Browse files
committed
Add examples
1 parent 3ec586b commit 74f024d

File tree

6 files changed

+161
-0
lines changed

6 files changed

+161
-0
lines changed

JavaScript/1-iterator.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const iterator = {
4+
counter: 0,
5+
next() {
6+
return {
7+
value: this.counter++, // current value
8+
done: this.counter > 3 // boolean
9+
};
10+
}
11+
};
12+
13+
const step1 = iterator.next();
14+
const step2 = iterator.next();
15+
const step3 = iterator.next();
16+
const step4 = iterator.next();
17+
console.log({ step1, step2, step3, step4 });

JavaScript/2-iterable.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const iterable = {
4+
[Symbol.iterator]() {
5+
let i = 0;
6+
const iterator = {
7+
next() {
8+
return {
9+
value: i++,
10+
done: i > 3
11+
};
12+
}
13+
};
14+
return iterator;
15+
}
16+
};
17+
18+
// Usage
19+
20+
const iterator = iterable[Symbol.iterator]();
21+
const step1 = iterator.next();
22+
const step2 = iterator.next();
23+
const step3 = iterator.next();
24+
const step4 = iterator.next();
25+
console.log({ step1, step2, step3, step4 });
26+
27+
for (const step of iterable) {
28+
console.log({ step });
29+
}
30+
31+
console.log({ steps: [...iterable] });

JavaScript/3-class.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
class Counter {
4+
constructor(begin, end, step = 1) {
5+
this.begin = begin;
6+
this.end = end;
7+
this.step = step;
8+
}
9+
[Symbol.iterator]() {
10+
const end = this.end;
11+
let i = this.begin;
12+
const iterator = {
13+
next() {
14+
return {
15+
value: i++,
16+
done: i > end
17+
};
18+
}
19+
};
20+
return iterator;
21+
}
22+
}
23+
24+
// Usage
25+
26+
const iterable = new Counter(0, 3);
27+
28+
const iterator = iterable[Symbol.iterator]();
29+
const step1 = iterator.next();
30+
const step2 = iterator.next();
31+
const step3 = iterator.next();
32+
const step4 = iterator.next();
33+
console.log({ step1, step2, step3, step4 });
34+
35+
for (const step of iterable) {
36+
console.log({ step });
37+
}
38+
39+
console.log({ steps: [...iterable] });

JavaScript/4-array.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const iterable = [0, 1, 2];
4+
5+
const iterator = iterable[Symbol.iterator]();
6+
const step1 = iterator.next();
7+
const step2 = iterator.next();
8+
const step3 = iterator.next();
9+
const step4 = iterator.next();
10+
console.log({ step1, step2, step3, step4 });
11+
12+
for (const step of iterable) {
13+
console.log({ step });
14+
}
15+
16+
console.log({ steps: [...iterable] });

JavaScript/5-generator.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const gen = function* () {
4+
let i = 0;
5+
while (true) {
6+
if (i >= 3) return;
7+
yield i++;
8+
}
9+
};
10+
11+
{
12+
const iterable = gen();
13+
const iterator = iterable[Symbol.iterator]();
14+
const step1 = iterator.next();
15+
const step2 = iterator.next();
16+
const step3 = iterator.next();
17+
const step4 = iterator.next();
18+
console.log({ step1, step2, step3, step4 });
19+
}
20+
21+
{
22+
const iterable = gen();
23+
for (const step of iterable) {
24+
console.log({ step });
25+
}
26+
}
27+
28+
{
29+
const iterable = gen();
30+
console.log({ steps: [...iterable] });
31+
}

JavaScript/6-yield.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const gen = function* () {
4+
yield* [0, 1, 2];
5+
};
6+
7+
{
8+
const iterable = gen();
9+
const iterator = iterable[Symbol.iterator]();
10+
const step1 = iterator.next();
11+
const step2 = iterator.next();
12+
const step3 = iterator.next();
13+
const step4 = iterator.next();
14+
console.log({ step1, step2, step3, step4 });
15+
}
16+
17+
{
18+
const iterable = gen();
19+
for (const step of iterable) {
20+
console.log({ step });
21+
}
22+
}
23+
24+
{
25+
const iterable = gen();
26+
console.log({ steps: [...iterable] });
27+
}

0 commit comments

Comments
 (0)