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
怠惰なRubyistへの道 - Enumerator::Lazy の使いかた@福岡Ruby会議01
Search
nagachika
December 02, 2012
Programming
5
2.1k
怠惰なRubyistへの道 - Enumerator::Lazy の使いかた@福岡Ruby会議01
福岡Ruby会議01での発表資料を公開用にPDFに変換したものです。アニメーションやデモの動画はスライドによる説明に置き換えています。
nagachika
December 02, 2012
Tweet
Share
More Decks by nagachika
See All by nagachika
Functional Music Composition
nagachika
1
640
ruby trunk changes 統計版
nagachika
3
240
怠惰なRubyistへの道 - Enumerator::Lazy の使いかた
nagachika
17
7.5k
Ruby Kaja のご提案
nagachika
8
2k
Other Decks in Programming
See All in Programming
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
色々なIaCツールを実際に触って比較してみる
iriikeita
0
330
CSC509 Lecture 11
javiergs
PRO
0
180
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
140
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
Contemporary Test Cases
maaretp
0
130
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1k
Remix on Hono on Cloudflare Workers
yusukebe
1
280
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
660
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
330
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
1.8k
Featured
See All Featured
Bash Introduction
62gerente
608
210k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
Facilitating Awesome Meetings
lara
50
6.1k
The Invisible Side of Design
smashingmag
298
50k
How GitHub (no longer) Works
holman
310
140k
Ruby is Unlike a Banana
tanoku
97
11k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Transcript
ଵଦͳ3VCZJTUͷಓ &OVNFSBUPS-B[Zͷ͍͔ͨ $IJLBOBHB5PNPZVLJ !OBHBDIJLB 'VLVPLBSC 'VLVPLB3VCZ,BJHJ
"HFOEB &OVNFSBCMF &OVNFSBUPS &OVNFSBUPS-B[Z
"HFOEB &OVNFSBCMF ͷ৽ػೳ /&8 &OVNFSBUPS &OVNFSBUPS-B[Z
"55&/5*0/ ࠓͷ 3VCZͷ ৽ػೳʹ͍ͭͯ
3VCZ ϦϦʔε༧ఆ <SVCZEFW>ΑΓ "VHCJHGFBUVSFGSFF[F 0DUGFBUVSFGSFF[F 'FCSFMFBTF 3VCZͷੜप
3VCZ "VHCJHGFBUVSFGSFF[F 0DUGFBUVSFGSFF[F /PWQSFWJFXSFMFBTFE ˡࠓίί %FDQSFWJFXSFMFBTFE 'FCSFMFBTF
JTJO EFWFMPQNFOU ˞˞ ༷։ൃதͷͷͰ͋Γ ϦϦʔε൛Ͱมߋ͞ΕΔ߹͕͋Γ·͢
*OTUBMM3VCZ ͰະདྷΛ֞ؒݟΔ SWN SCFOWΛ͍ͬͯΕ SWNJOTUBMMSVCZIFBE SCFOWJOTUBMMEFWFM
4FMG*OUSPEVDUJPO UXJUUFSJE!OBHBDIJLB SVCZUSVOLDIBOHFT IUUQEIBUFOBOFKQOBHBDIJLB $3VCZDPNNJUUFS :PLPIBNBSCˠ'VLVPLBSC 4PVOESC SVCZDPSFBVEJP SVCZQVSFEBUB
3FDFOU"DUJWJUZ 3%PDˠ 3VCZ(FNTˠ 3BLFˠ 3BLF .JOJ5FTUˠ ۭલͷϝδϟʔόʔδϣ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0 &OVNFSBCMFΛ JODMVEF͍ͯ͠Δ
&OVNFSBCMF ʲܗ༰ࢺʳ͑ΒΕΔ<ຊޠ8PSE/FU ӳ ՄࢉʀՄ൪<Ϋϩεϥϯήʔδઐޠࣙॻ> $BQBCMFPGCFJOHFOVNFSBUFE DPVOUBCMF<8JLJUJPOBSZ>
&OVNFSBCMF ʮFBDIϝιουΛݺͿͱɺԿ͔͕ ॱʹ /ճ ZJFMEͰ͞ΕΔʯ ͱ͍͏ϧʔϧ /
&OVNFSBCMF FBDIϝιου͕ఆٛ͞Εͯ ͍ΔΫϥεʹJODMVEFͯ͠ ͏ .JYJO
&OVNFSBCMF FBDI͚ͩ༻ҙ͓͚ͯ͠ NBQ TFMFDU SFKFDU HSFQ FUD ͱ͍ͬͨϝιου͕͑ΔΑ͏ʹͳΔ ͜ΕΒͷϝιου͕FBDIΛͬͯ ࣮͞Ε͍ͯΔ
"O&YBNQMF DMBTT" JODMVEF&OVNFSBCMF EFGFBDI ZJFME ZJFME ZJFME FOE FOE "OFXNBQ\]J]J
^< >
͜͜·Ͱͷ·ͱΊ &OVNFSBCMFFBDIϝιουΛͬͯ ଟ࠼ͳϝιουΛՃ͢Δ ͳʹ͕ʮ্͑͛ΒΕΔʯͷ͔ FBDIϝιου͕ͲͷΑ͏ʹ ZJFMEΛݺͼग़͔͢Ͱܾ·Δ
&OVNFSBUPS &OVNFSBUPS ͬͯ·͢
&OVNFSBUPS FBDIʹ͢ϒϩοΫΛอཹͨ͠ ঢ়ଶͰΦϒδΣΫτͱͯ࣋ͪ͠ճΔ
UP@FOVN FOVN@GPS &OVNFSBUPSͷ࡞Γ͔ͨ w &OVNFSBUPSOFX PCK NFUIPEFBDI BSHT w
PCKUP@FOVN NFUIPEFBDI BSHT w PCKFOVN@GPS NFUIPEFBDI BSHT w &OVNFSBUPSOFX\]Z]^ ˢ͜Ε͚ͩͪΐͬͱಛघ
0CKFDUFOVN@GPS FBDIҎ֎ͷҙͷϝιουͷݺͼग़͠Λ &OVNFSBUPSʹͰ͖Δ ࣮&OVNFSBCMFͰͳ͍͍ͯ͘ F@CZUFTUEJOFOVN@GPS FBDI@CZUF όΠτ͝ͱʹZJFME F@MJOFTUEJOFOVN@GPS FBDI@MJOF ߦ͝ͱʹZJFME
&OVNFSBUPSOFX\]Z]^ F&OVNFSBUPSOFX\]Z] Z ZGPP Z ^ ϒϩοΫ͕FBDIͷΘΓɻ ϒϩοΫύϥϝʔλZʹ͢Δͱ ZJFME͢Δ͜ͱʹͳΔɻ
&OVNFSBUPSOFX\]Z]^ DMBTT" EFGFBDI ZJFME ZJFMECBS ZJFME FOE FOE "OFXUP@FOVN ͱಉ͡
&OVNFSBUPSBT &YUFSOBM*UFSBUPS ֎෦ΠςϨʔλͱͯ͑͠Δ F UP@FOVN FOFYUFBDI͕ZJFME͢Δ͕ FOFYUॱʹOFYUͰऔΓग़ͤΔ FOFYU FOFYU
4UPQ*UFSBUJPOJUFSBUJPOSFBDIFEBOFOE
.FUIPE$IBJO NBQ TFMFDUͳͲҰ෦ͷϝιουϒϩο ΫΛলུ͢Δͱ&OVNFSBUPSΛฦ͢ͷͰ ෳͷϝιουΛΈ߹Θͤͯ͏ BSZNBQXJUI@JOEFY\]W J]^
.FUIPE$IBJO <B C D E>NBQXJUI@JOEFYEP]TZN J] JFWFO TZNJ FOE
<B D > &OVNFSBUPS ಠࣗͷϝιου
XJUI@JOEFY <SVCZEFW>.BU[XSPUF lNBQ͕FOVNFSBUPSΛฦ͢ͱ PCKNBQXJUI@JOEFY\]Y J]^ ͕Ͱ͖Δͷ͕خ͍͠ͷͰಋೖͨ͠ͷͰͨ͠ɻͱ͍͏Θ͚Ͱɺ FBDIʹNBQʹXJUI@JOEFY͕͚ΒΕͯخ͍͠ ͱ͍͏ͷ͕ຊͷཧ༝Ͱ͢ɻz
4FDSFU4UPSZPG &OVNFSBUPS &OVNFSBUPS XJUI@JOEFYͷҝʹ ੜ·Εͨ
͜͜·Ͱͷ·ͱΊ w&OVNFSBUPS w֎෦ΠςϨʔλԽ wϝιουνΣʔϯ w
&OVNFSBUPS-B[Z ͷ৽ػೳ
&OVNFSBUPS-B[Z &OVNFSBUPSͷαϒΫϥε
&OVNFSBCMFMB[Z &OVNFSBUPS-B[Zͷ࡞Γ͔ͨ &OVNFSBCMFMB[ZΛݺͿ MB[Z &OVNFSBUPS-B[Z
&OVNFSBUPS-B[Z -B[ZͷNBQ TFMFDU [JQͳͲҰ෦ͷϝιο υ͕͞Βʹ-B[ZΛฦ͢ MB[ZNBQ\]J]J ^ &OVNFSBUPS-B[Z
&OVNFSBUPS-B[ZNBQ
&OVNFSBUPS-B[Z > Enumerator::Lazy.instance_methods(false) map collect flat_map collect_concat select find_all reject
grep zip take take_while drop drop_wihle cycle lazy force [ruby 2.0.0dev (2012-05-30 trunk 35844)]
GPSDF &OVNSBUPS-B[ZGPSDF UP@BͷBMJBT Ԇ͞Ε͍ͯΔΠςϨʔλͷධՁΛ࣮ߦ
"-B[Z%FNP >> l = (0..5).lazy => <Enumerator::Lazy: 0..5> >> l_map
= l.map{|i| p i; i * 2 } => <Enumerator::Lazy: #<Enumerator::Lazy: 0..5>:map> # ͜ͷ࣌Ͱ·ͩϒϩοΫͷ༰࣮ߦ͞Ε͍ͯͳ͍! >> l_map.force 0 1 2 3 4 5 => [0, 2, 4, 6, 8, 10] # force ΛݺͿͱ࣮ߦ͞ΕΔ
&OVNFSBUPSͱͷҧ͍ .FUIPE$IBJOͷ͍͔͕ͨΑΓڧྗʹ தؒσʔλͷ੍
1JUGBMMPG &OVNFSBUPS NBQ TFMFDUͷΑ͏ʹϒϩοΫͷΓ Λར༻͢ΔϝιουΛෳܨ͛Δ͜ͱ͕Ͱ ͖ͳ͍ ͯ͠ҙຯ͕ͳ͘ͳΔ NBQTFMFDU\]J]J^
< > ˢNBQͷҙຯ͕ͳ͘ͳ͍ͬͯΔ
5SVF.FUIPE$IBJO NBQTFMFDUͱ͍ͬͨϝιου͍ͭ͘Ͱ .FUIPE$IBJO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MFDU\^GPSDF ($`FE
-B[ZWFSTJPO MBSHF@BSSBZMB[ZNBQ\^TFMFDU\^GPSDF
-B[ZWFSTJPO MBSHF@BSSBZMB[ZNBQ\^TFMFDU\^GPSDF தؒσʔλ͕ෆཁ
-B[Z$IBJO%FNP >> (0..4).lazy.select{|i| p “select:#{i}”; i.event? }.map{|i| p “map:#{i}”; i
* 2 }.force “select:0” “map:0” “select:1” “select:2” “map:2” “select:3” “select:4” “map:4” => [0, 4, 8]
-B[ZXJUI*0 ྫ TUEJO͔ΒۭߦΛಡΉ·Ͱͷ֤ߦͷ จࣈͷྻΛฦ͢ TUEJOMB[ZUBLF@XIJMF\]M] MDIPNQFNQUZ ^NBQ\]M]MDIPNQTJ[F^
-B[ZXJUI*0 &OVNFSBCMF &OVNFSBUPSͩͱۭߦ·Ͱઌ ʹಡΜͰ͠·͏ɻ -B[ZͳΒߦಡΉͨͼʹUBLF@XIJMFNBQ ͷϒϩοΫ͕࣮ߦ͞ΕΔ ˠஞ࣍ॲཧͰ͖Δ
-B[Z-JTU ແݶʹଓ͘ཁૉΛѻ͑Δ MJTU 'MPBU*/'*/*5: MJTUNBQ\]J]J ^UBLF ฦͬͯ͜ͳ͍ MJTUMB[ZNBQ\]J]J ^UBLF
GPSDF < >
*OUFSBDUJWJUZ ࣮ߦॱংΛར༻ͯ͠ΠϯλϥΫςΟϒʹ 'MPBU*/'*/*5: MB[ZNBQ\]J] QOVN MJOFTUEJOHFUT MJOFDIPNQJGMJOF ^UBLF@XIJMF\]M] MBOEOPUMFNQUZ ^GPSDF
1JUGBMMPG-B[Z ϝϞԽ͞Εͳ͍ GPSDFΛճݺͿͱ͏ҰFBDI͕ ݺΕΔ *0ͳͲ෭࡞༻ͷ͋ΔFBDIͩͱ݁Ռ͕ มԽ͢Δ FY TUEJOMB[ZUBLF
FOVN@GPSͷΑ͏ʹFBDIͷ͔ΘΓʹͳ ΔϝιουΛࢦఆͰ͖ͳ͍ TUEJOFOVN@GPS FBDI@CZUF MB[Z TUEJOMB[Z FBDI@CZUF ͱॻ͖͍ͨ
-B[ZBTB#FUUFS &OVNFSBUPS -B[Z &OVNFSBUPSͷ ਖ਼౷ਐԽ൛
)BQQZ-B[Z 1SPHSBNNJOH