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
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
1.3k
traP の部内 ISUCON とそれを支えるポータル / PISCON Portal
ikura_hamu
0
180
いりゃあせ、PHPカンファレンス名古屋2025 / Welcome to PHP Conference Nagoya 2025
ttskch
1
150
ある日突然あなたが管理しているサーバーにDDoSが来たらどうなるでしょう?知ってるようで何も知らなかったDDoS攻撃と対策 #phpcon.2024
akase244
2
7.7k
Lookerは可視化だけじゃない。UIコンポーネントもあるんだ!
ymd65536
1
130
DevinとCursorから学ぶAIエージェントメモリーの設計とMoatの考え方
itarutomy
0
110
Simple組み合わせ村から大都会Railsにやってきた俺は / Coming to Rails from the Simple
moznion
3
2.1k
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
1.3k
return文におけるstd::moveについて
onihusube
1
1.4k
2025.01.17_Sansan × DMM.swift
riofujimon
2
530
.NETでOBS Studio操作してみたけど…… / Operating OBS Studio by .NET
skasweb
0
120
サーバーゆる勉強会 DBMS の仕組み編
kj455
1
300
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
500
Documentation Writing (for coders)
carmenintech
67
4.5k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
The Pragmatic Product Professional
lauravandoore
32
6.4k
Mobile First: as difficult as doing things right
swwweet
222
9k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
Building Adaptive Systems
keathley
38
2.4k
Embracing the Ebb and Flow
colly
84
4.5k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
950
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 SVCZDPSF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PS-B[Z
&OVNFSBUPS-B[Z ͷલʹ
&OVNFSBCMF
&OVNFSBCMF ͪΖΜͬͯ·͢ΑͶ
"SSBZ )BTI *0 "SSBZ)BTI *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^
< >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EJOMB[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