function range(a, b, s = 1) {
const arr = [];
for (let n = a; n < b; n += s) {
arr.push(n);
}
return arr;
}
!
for (const n of range(1, 10)) {
doSomething(n); // from 1 to 9
}
Slide 7
Slide 7 text
function* xrange(a, b, s = 1) {
for (let n = a; n < b; n += s) {
yield n;
}
}
!
for (const n of xrange(1, 10)) {
doSomething(n); // from 1 to 9
}
Slide 8
Slide 8 text
function* fibonacci() {
let [a, b] = [1, 1];
while (true) {
yield a;
[a, b] = [b, a + b];
}
}
!
for (const n of fibonacci()) {
doSomething(n); // 1, 1, 2, 3, 5, ...
}
Slide 9
Slide 9 text
3 . Π ς Ϩ ʔ λ ͷ Ճ
Slide 10
Slide 10 text
function* concat(iter1, iter2) {
yield* iter1;
yield* iter2;
}
!
for (const x of concat(arr1, arr2)) {
doSomething(x);
}
Slide 11
Slide 11 text
function* map(func, iter) {
for (const x of iter) {
yield func(x);
}
}
!
for (const n of map(n => n ** 2, arr)) {
doSomething(n);
}
Slide 12
Slide 12 text
4 . ඇ ಉ ظ ॲ ཧ
Slide 13
Slide 13 text
fetch().then(x =>
fetch().then(y =>
fetch().then(z =>
doSomething(x, y, z)
)
)
);
before (Promise)
Slide 14
Slide 14 text
co(function* () {
const x = yield fetch();
const y = yield fetch();
const z = yield fetch();
return doSomething(x, y, z);
});
after (Promise + Generator + co)
Slide 15
Slide 15 text
Q .
ଞ ʹ δΣω Ϩ ʔ λ ͕
͑Δ ໘ ͋ Δʁ
Slide 16
Slide 16 text
ͦ ͦ δΣω Ϩ ʔ λ ͱ
Slide 17
Slide 17 text
g e n . n e x t ( )
Slide 18
Slide 18 text
{ d o n e : f a l s e ,
v a l u e : * }
g e n . n e x t ( )
Slide 19
Slide 19 text
{ d o n e : f a l s e ,
v a l u e : * }
g e n . n e x t ( )
g e n . n e x t ( )
gen.next ͷதมΘ͍ͬͯΔ
Slide 20
Slide 20 text
{ d o n e : f a l s e ,
v a l u e : * }
{ d o n e : f a l s e ,
v a l u e : * }
g e n . n e x t ( )
g e n . n e x t ( )
g e n . n e x t ( )
Slide 21
Slide 21 text
{ d o n e : f a l s e ,
v a l u e : * }
{ d o n e : f a l s e ,
v a l u e : * }
{ d o n e : t r u e ,
v a l u e : * }
g e n . n e x t ( )
g e n . n e x t ( )
g e n . n e x t ( )
Slide 22
Slide 22 text
ݟํΛม͑Δͱ͍͔ʹԆϦετ
d o n e : f a l s e
v a l u e ( ) = > d o n e : f a l s e
v a l u e ( ) = > d o n e : t r u e
v a l u e
Slide 23
Slide 23 text
d o n e : f a l s e
v a l u e x = > d o n e : f a l s e
v a l u e y = > d o n e : t r u e
v a l u e
.next() ҾΛͱΕΔ