$30 off During Our Annual Pro Sale. View Details »

ジェネレータを有効活用し隊 / 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

    View Slide

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

    View Slide

  3. 1 . ί ϧ ʔ νϯ

    View Slide

  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

    View Slide

  5. 2 . ஗ Ԇ Ϧε τ

    View Slide

  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
    }

    View Slide

  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
    }

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  12. 4 . ඇ ಉ ظ ॲ ཧ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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 ( )

    View Slide

  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 ( )

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  25. View Slide

  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

    View Slide

  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() Ͱͭͳ͗߹ΘͤΔ

    View Slide

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

    View Slide

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

    View Slide

  30. A .
    ͋ Γ · ͢

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide