Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
ジェネレータを有効活用し隊 / Kyoto.js 11 LT
Susisu
October 22, 2016
Programming
2
1.7k
ジェネレータを有効活用し隊 / Kyoto.js 11 LT
Kyoto.js 11
http://kyotojs.connpass.com/event/39462/
Susisu
October 22, 2016
Tweet
Share
More Decks by Susisu
See All by Susisu
Mackerel のフロントエンドフレームワーク移行 序章 / Hatena Engineer Seminar #13
susisu
0
1.7k
スクリーンショット撮影のために Puppeteer を操る / Kyoto.js 16
susisu
0
560
BuckleScript 使ってみた
susisu
0
220
Atom パッケージ開発のすゝめ
susisu
1
1.8k
5分でわかる Curry–Howard 同型対応
susisu
0
400
遅延評価と健康
susisu
0
480
楽しく学ぶ難解プログラミング言語
susisu
0
480
多分これが一番早いと思います
susisu
0
340
私を SKI に連れてって
susisu
0
240
Other Decks in Programming
See All in Programming
Unity Localization で多言語対応実装しよう / xrdnk-yokohamaunity-lt10-20220513
xrdnk
0
190
アプリのログをチーム外で活用してもらうためにやったこと
shotakashihara
0
200
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
1.2k
roadmap to rust 2024
matsu7874
1
910
「新卒だけ」じゃない!学び直しを支えるミクシィの技術研修を紹介
mixi_engineers
PRO
0
280
UI State Modeling 어떤게 좋을까?
laco2951
1
250
ソフトウェアテストなんて他人事! だと思っていた私が始めた小さな取り組み
izumii19
0
470
Architectural practices for greater scalability and innovation
otaviojava
0
140
マイクロサービスプラットフォーム向け負荷試験基盤の初期リリースを終えた話
yuyu_hf
PRO
1
500
Learning DDD輪読会#4 / Learning DDD Book Club #4
suzushin54
1
160
WindowsコンテナDojo:第2回 Windowsコンテナアプリのビルド、公開、デプロイ
oniak3ibm
PRO
0
160
byte列のbit表現を得るencodingライブラリ作った
convto
1
210
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
24
6.2k
A better future with KSS
kneath
225
15k
Robots, Beer and Maslow
schacon
152
7.1k
A designer walks into a library…
pauljervisheath
196
16k
Building Flexible Design Systems
yeseniaperezcruz
310
33k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
268
11k
Visualization
eitanlees
124
11k
A Modern Web Designer's Workflow
chriscoyier
689
180k
Streamline your AJAX requests with AmplifyJS and jQuery
dougneiner
125
8.5k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
350
21k
Intergalactic Javascript Robots from Outer Space
tanoku
261
25k
Principles of Awesome APIs and How to Build Them.
keavy
113
15k
Transcript
δΣω Ϩ ʔ λ Λ ༗ ޮ ׆ ༻ ͠
ୂ 2 0 1 6 - 1 0 - 2 2 K y o t o . j s 1 1 Twitter @susisu2413 / GitHub @susisu
Q . δΣω Ϩ ʔ λ Կ ʹ
͑Δʁ
1 . ί ϧ ʔ νϯ
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
2 . Ԇ Ϧε τ
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 }
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 }
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, ... }
3 . Π ς Ϩ ʔ λ ͷ Ճ
function* concat(iter1, iter2) { yield* iter1; yield* iter2; } !
for (const x of concat(arr1, arr2)) { doSomething(x); }
function* map(func, iter) { for (const x of iter) {
yield func(x); } } ! for (const n of map(n => n ** 2, arr)) { doSomething(n); }
4 . ඇ ಉ ظ ॲ ཧ
fetch().then(x => fetch().then(y => fetch().then(z => doSomething(x, y, z) )
) ); before (Promise)
co(function* () { const x = yield fetch(); const y
= yield fetch(); const z = yield fetch(); return doSomething(x, y, z); }); after (Promise + Generator + co)
Q . ଞ ʹ δΣω Ϩ ʔ λ ͕
͑Δ ໘ ͋ Δʁ
ͦ ͦ δΣω Ϩ ʔ λ ͱ
g e n . n e x t ( )
{ d o n e : f a l s
e , v a l u e : * } g e n . n e x t ( )
{ 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 ͷதมΘ͍ͬͯΔ
{ 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 ( )
{ 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 ( )
ݟํΛม͑Δͱ͍͔ʹԆϦετ 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
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() ҾΛͱΕΔ
fetch().then(x => fetch().then(y => fetch().then(z => doSomething(x, y, z) )
) ); PromiseΛͬͨඇಉظॲཧ
None
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
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() Ͱͭͳ͗߹ΘͤΔ
co(function* () { const x = yield fetch(); const y
= yield fetch(); const z = yield fetch(); return doSomething(x, y, z); }); Promise + Generator + co
Q . ଞ ʹ δΣω Ϩ ʔ λ ͕
͑Δ ໘ ͋ Δʁ
A . ͋ Γ · ͢
5 . ύ ʔ α ί ϯ Ϗω ʔ λ
symbol("(").bind(_ => expr.bind(x => symbol(")").bind(_ => pure(x) ) ) );
ׅހ (ʙ) ʹೖͬͨࣜͷύʔα
qo(function* () { yield symbol("("); const x = yield expr;
yield symbol(")"); return x; }); దͳؔΛ͑……
6 . * ద ͳ Ϟ φ υ ※
Ͳ ΕͰ ྑ ͍ ͱ ͍ ͏ Θ ͚ Ͱ ͳ ͍
δΣω Ϩ ʔ λ Λ ༗ ޮ ׆ ༻ ͠
ୂ ׆ ಈ ใ ࠂ
δΣω Ϩ ʔ λ Ԇ Ϧε τ Λ
Ұ ൠ Խ ͠ ͨ Α ͏ ͳ ͷ
· ͩ · ͩ ͑Δ ໘ ͕ ͋
Γ ͦ ͏ ྫ : ύ ʔ α ί ϯ Ϗω ʔ λ
ଞ ʹ ׆ ༻ ࣄ ྫ ͕ ͋ Ε
͓ ڭ ͑͘ ͩ ͞ ͍
{ d o n e : t r u e
, v a l u e : " T h a n k s " }