Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

1 . ί ϧ ʔ νϯ

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

2 . ஗ Ԇ Ϧε τ

Slide 6

Slide 6 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

A . ͋ Γ · ͢

Slide 31

Slide 31 text

5 . ύ ʔ α ί ϯ Ϗω ʔ λ

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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