Susisu
October 22, 2016
1.9k

# ジェネレータを有効活用し隊 / Kyoto.js 11 LT

October 22, 2016

## Transcript

1. δΣω Ϩ ʔ λ Λ
༗ ޮ ׆ ༻ ͠ ୂ
2 0 1 6 - 1 0 - 2 2 K y o t o . j s 1 1

2. Q .
δΣω Ϩ ʔ λ ͸
Կ ʹ ࢖ ͑Δʁ

3. 1 . ί ϧ ʔ νϯ

4. app.use(function* (next) {
const start = new Date(); // (1)
yield next;
const end = new Date(); // (3)
// log (4)
});
!
app.use(function* (next) {
yield next;
// respond (2)
});
ྫ: Koa

5. 2 . ஗ Ԇ Ϧε τ

6. 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
}

7. 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
}

8. 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, ...
}

9. 3 . Π ς Ϩ ʔ λ ͷ Ճ ޻

10. function* concat(iter1, iter2) {
yield* iter1;
yield* iter2;
}
!
for (const x of concat(arr1, arr2)) {
doSomething(x);
}

11. function* map(func, iter) {
for (const x of iter) {
yield func(x);
}
}
!
for (const n of map(n => n ** 2, arr)) {
doSomething(n);
}

12. 4 . ඇ ಉ ظ ॲ ཧ

13. fetch().then(x =>
fetch().then(y =>
fetch().then(z =>
doSomething(x, y, z)
)
)
);
before (Promise)

14. co(function* () {
const x = yield fetch();
const y = yield fetch();
const z = yield fetch();
return doSomething(x, y, z);
});
after (Promise + Generator + co)

15. Q .
ଞ ʹ ΋ δΣω Ϩ ʔ λ ͕
࢖ ͑Δ ৔ ໘ ͸ ͋ Δʁ

16. ͦ ΋ ͦ ΋ δΣω Ϩ ʔ λ ͱ ͸

17. g e n . n e x t ( )

18. { d o n e : f a l s e ,
v a l u e : * }
g e n . n e x t ( )

19. { 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 ͷத਎΋มΘ͍ͬͯΔ

20. { 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 ( )

21. { 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 ( )

22. ݟํΛม͑Δͱ͍͔ʹ΋஗ԆϦετ
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

23. 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() ͸Ҿ਺ΛͱΕΔ

24. fetch().then(x =>
fetch().then(y =>
fetch().then(z =>
doSomething(x, y, z)
)
)
);
PromiseΛ࢖ͬͨඇಉظॲཧ

25. 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

26. t h e n
v a l u e x = > t h e n
v a l u e y = > re s o l v e
v a l u e
.then() Ͱͭͳ͗߹ΘͤΔ

27. co(function* () {
const x = yield fetch();
const y = yield fetch();
const z = yield fetch();
return doSomething(x, y, z);
});
Promise + Generator + co

28. Q .
ଞ ʹ ΋ δΣω Ϩ ʔ λ ͕
࢖ ͑Δ ৔ ໘ ͸ ͋ Δʁ

29. A .
͋ Γ · ͢

30. 5 . ύ ʔ α ί ϯ Ϗω ʔ λ

31. symbol("(").bind(_ =>
expr.bind(x =>
symbol(")").bind(_ =>
pure(x)
)
)
);
ׅހ (ʙ) ʹೖͬͨࣜͷύʔα

32. qo(function* () {
yield symbol("(");
const x = yield expr;
yield symbol(")");
return x;
});
ద౰ͳؔ਺Λ࢖͑͹……

33. 6 . * ద ౰ ͳ Ϟ φ υ
※ Ͳ ΕͰ ΋ ྑ ͍ ͱ ͍ ͏ Θ ͚ Ͱ ͸ ͳ ͍

34. δΣω Ϩ ʔ λ Λ
༗ ޮ ׆ ༻ ͠ ୂ
׆ ಈ ใ ࠂ

35. δΣω Ϩ ʔ λ ͸
஗ Ԇ Ϧε τ Λ
Ұ ൠ Խ ͠ ͨ Α ͏ ͳ ΋ ͷ

36. · ͩ · ͩ
࢖ ͑Δ ৔ ໘ ͕ ͋ Γ ͦ ͏
ྫ : ύ ʔ α ί ϯ Ϗω ʔ λ

37. ଞ ʹ ׆ ༻ ࣄ ྫ ͕ ͋ Ε ͹
͓ ڭ ͑͘ ͩ ͞ ͍

38. { d o n e : t r u e ,
v a l u e : " T h a n k s " }