Save 37% off PRO during our Black Friday Sale! »

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

95d12ef3ae39f388be5a43b4788cb70e?s=47 Susisu
October 22, 2016

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

95d12ef3ae39f388be5a43b4788cb70e?s=128

Susisu

October 22, 2016
Tweet

Transcript

  1. δΣω Ϩ ʔ λ Λ ༗ ޮ ׆ ༻ ͠

    ୂ 2 0 1 6 - 1 0 - 2 2 K y o t o . j s 1 1 Twitter @susisu2413 / GitHub @susisu
  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. None
  26. 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
  27. 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() Ͱͭͳ͗߹ΘͤΔ
  28. co(function* () { const x = yield fetch(); const y

    = yield fetch(); const z = yield fetch(); return doSomething(x, y, z); }); Promise + Generator + co
  29. Q . ଞ ʹ ΋ δΣω Ϩ ʔ λ ͕

    ࢖ ͑Δ ৔ ໘ ͸ ͋ Δʁ
  30. A . ͋ Γ · ͢

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

  32. symbol("(").bind(_ => expr.bind(x => symbol(")").bind(_ => pure(x) ) ) );

    ׅހ (ʙ) ʹೖͬͨࣜͷύʔα
  33. qo(function* () { yield symbol("("); const x = yield expr;

    yield symbol(")"); return x; }); ద౰ͳؔ਺Λ࢖͑͹……
  34. 6 . * ద ౰ ͳ Ϟ φ υ ※

    Ͳ ΕͰ ΋ ྑ ͍ ͱ ͍ ͏ Θ ͚ Ͱ ͸ ͳ ͍
  35. δΣω Ϩ ʔ λ Λ ༗ ޮ ׆ ༻ ͠

    ୂ ׆ ಈ ใ ࠂ
  36. δΣω Ϩ ʔ λ ͸ ஗ Ԇ Ϧε τ Λ

    Ұ ൠ Խ ͠ ͨ Α ͏ ͳ ΋ ͷ
  37. · ͩ · ͩ ࢖ ͑Δ ৔ ໘ ͕ ͋

    Γ ͦ ͏ ྫ : ύ ʔ α ί ϯ Ϗω ʔ λ
  38. ଞ ʹ ׆ ༻ ࣄ ྫ ͕ ͋ Ε ͹

    ͓ ڭ ͑͘ ͩ ͞ ͍
  39. { d o n e : t r u e

    , v a l u e : " T h a n k s " }