Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Susisu
October 22, 2016

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

Susisu

October 22, 2016
Tweet

More Decks by Susisu

Other Decks in Programming

Transcript

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

    ୂ 2 0 1 6 - 1 0 - 2 2 K y o t o . j s 1 1 Twitter @susisu2413 / GitHub @susisu
  2. 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
  3. 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 }
  4. 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 }
  5. 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, ... }
  6. function* concat(iter1, iter2) { yield* iter1; yield* iter2; } !

    for (const x of concat(arr1, arr2)) { doSomething(x); }
  7. function* map(func, iter) { for (const x of iter) {

    yield func(x); } } ! for (const n of map(n => n ** 2, arr)) { doSomething(n); }
  8. co(function* () { const x = yield fetch(); const y

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

    ࢖ ͑Δ ৔ ໘ ͸ ͋ Δʁ
  10. { d o n e : f a l s

    e , v a l u e : * } g e n . n e x t ( )
  11. { 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 ͷத਎΋มΘ͍ͬͯΔ
  12. { 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 ( )
  13. { 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 ( )
  14. ݟํΛม͑Δͱ͍͔ʹ΋஗ԆϦετ 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
  15. 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() ͸Ҿ਺ΛͱΕΔ
  16. 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
  17. 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() Ͱͭͳ͗߹ΘͤΔ
  18. co(function* () { const x = yield fetch(); const y

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

    ࢖ ͑Δ ৔ ໘ ͸ ͋ Δʁ
  20. qo(function* () { yield symbol("("); const x = yield expr;

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

    Ͳ ΕͰ ΋ ྑ ͍ ͱ ͍ ͏ Θ ͚ Ͱ ͸ ͳ ͍
  22. δΣω Ϩ ʔ λ ͸ ஗ Ԇ Ϧε τ Λ

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

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

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

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