Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
ジェネレータを有効活用し隊 / Kyoto.js 11 LT
Search
Susisu
October 22, 2016
Programming
2
2.1k
ジェネレータを有効活用し隊 / 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
null or undefined
susisu
25
6.9k
Mackerel のフロントエンドフレームワーク移行 序章 / Hatena Engineer Seminar #13
susisu
0
2k
スクリーンショット撮影のために Puppeteer を操る / Kyoto.js 16
susisu
0
800
BuckleScript 使ってみた
susisu
0
290
Atom パッケージ開発のすゝめ
susisu
1
2.1k
5分でわかる Curry–Howard 同型対応
susisu
0
910
遅延評価と健康
susisu
0
580
楽しく学ぶ難解プログラミング言語
susisu
0
740
多分これが一番早いと思います
susisu
0
400
Other Decks in Programming
See All in Programming
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
190
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
120
Ethereum_.pdf
nekomatu
0
460
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
2k
Realtime API 入門
riofujimon
0
150
イベント駆動で成長して委員会
happymana
1
320
Contemporary Test Cases
maaretp
0
140
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
940
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
3
690
Featured
See All Featured
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Optimizing for Happiness
mojombo
376
70k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
The Language of Interfaces
destraynor
154
24k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
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 " }