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
660
ruby trunk changes 統計版
nagachika
3
240
怠惰なRubyistへの道 - Enumerator::Lazy の使いかた
nagachika
17
7.5k
Ruby Kaja のご提案
nagachika
8
2.1k
Other Decks in Programming
See All in Programming
Spatial Rendering for Apple Vision Pro
warrenm
0
110
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
550
命名をリントする
chiroruxx
1
410
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
testcontainers のススメ
sgash708
1
120
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
110
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
260
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
130
Mermaid x AST x 生成AI = コードとドキュメントの完全同期への道
shibuyamizuho
0
160
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
180
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
365
19k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Automating Front-end Workflow
addyosmani
1366
200k
Faster Mobile Websites
deanohume
305
30k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Code Review Best Practice
trishagee
65
17k
Adopting Sorbet at Scale
ufuk
73
9.1k
Navigating Team Friction
lara
183
15k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
For a Future-Friendly Web
brad_frost
175
9.4k
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ˠ ۭલͷϝδϟʔόʔδϣϯΞοϓϥογϡ
3FDFOU"DUJWJUZ ͔͚͜Έίϛοτϥογϡ
/FX'F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ʳ͑ΒΕΔ<ຊޠ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Ͱ͖Δɻ ͦ͏ɺ-B[ZͳΒͶɻ ͔Βͷ͏ͪҙͷܻʹ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DU\^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