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 の使いかた
Search
nagachika
June 02, 2012
Technology
17
7.5k
怠惰なRubyistへの道 - Enumerator::Lazy の使いかた
みなとRuby会議01での発表「怠惰なRubyistへの道 - Enumerator::Lazy の使いかた」の発表資料を配布用に編集したものです。
nagachika
June 02, 2012
Tweet
Share
More Decks by nagachika
See All by nagachika
Functional Music Composition
nagachika
1
650
怠惰なRubyistへの道 - Enumerator::Lazy の使いかた@福岡Ruby会議01
nagachika
5
2.1k
ruby trunk changes 統計版
nagachika
3
240
Ruby Kaja のご提案
nagachika
8
2k
Other Decks in Technology
See All in Technology
初心者向けAWS Securityの勉強会mini Security-JAWSを9ヶ月ぐらい実施してきての近況
cmusudakeisuke
0
130
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
0
110
New Relicを活用したSREの最初のステップ / NRUG OKINAWA VOL.3
isaoshimizu
3
630
OCI Security サービス 概要
oracle4engineer
PRO
0
6.5k
B2B SaaSから見た最近のC#/.NETの進化
sansantech
PRO
0
910
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
300k
SSMRunbook作成の勘所_20241120
koichiotomo
3
160
BLADE: An Attempt to Automate Penetration Testing Using Autonomous AI Agents
bbrbbq
0
320
100 名超が参加した日経グループ横断の競技型 AWS 学習イベント「Nikkei Group AWS GameDay」の紹介/mediajaws202411
nikkei_engineer_recruiting
1
170
AGIについてChatGPTに聞いてみた
blueb
0
130
日経電子版のStoreKit2フルリニューアル
shimastripe
1
150
VideoMamba: State Space Model for Efficient Video Understanding
chou500
0
190
Featured
See All Featured
Happy Clients
brianwarren
98
6.7k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
Teambox: Starting and Learning
jrom
133
8.8k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Testing 201, or: Great Expectations
jmmastey
38
7.1k
A Tale of Four Properties
chriscoyier
156
23k
Embracing the Ebb and Flow
colly
84
4.5k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
How to train your dragon (web standard)
notwaldorf
88
5.7k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Transcript
ଵଦͳ3VCZJTUͷಓ &OVNFSBUPS-B[Zͷ͍͔ͨ $IJLBOBHB5PNPZVLJ !OBHBDIJLB :PLPIBNBSC 1
"HFOEB &OVNFSBCMF &OVNFSBUPS &OVNFSBUPS-B[Z 2
8"3/*/( ࠓͷ 3VCZͷ ৽ػೳʹ͍ͭͯ 3
3VCZ ϦϦʔε༧ఆ <SVCZEFW>ΑΓ "VHCJHGFBUVSFGSFF[F 0DUGFBUVSFGSFF[F 'FCSFMFBTF 3VCZͷੜप 4
3VCZ ະདྷͷ 5
JTJO EFWFMPQNFOU ˞˞ ༷։ൃதͷͷͰ͋Γ ϦϦʔε൛Ͱมߋ͞ΕΔ߹͕͋Γ·͢ 6
*OTUBMM3VCZ ະདྷΛखதʹ SWN SCFOWΛ͍ͬͯΕ SWNJOTUBMMSVCZIFBE SCFOWJOTUBMMEFWFM 7
3VCZGPS 8JOEPXT 8
4FMG*OUSPEVDUJPO UXJUUFSJE!OBHBDIJLB SVCZUSVOLDIBOHFT IUUQEIBUFOBOFKQOBHBDIJLB $3VCZDPNNJUUFS :PLPIBNBSCˠԬԕத 4PVOESC SVCZDPSFBVEJP SVCZQVSFEBUB 9
&OVNFSBUPS-B[Z 10
&OVNFSBUPS-B[Z ͷલʹ 11
&OVNFSBCMF 12
&OVNFSBCMF ͪΖΜͬͯ·͢ΑͶ 13
"SSBZ )BTI *0 "SSBZ)BTI *0 &OVNFSBCMFΛ JODMVEF͍ͯ͠Δ 14
&OVNFSBCMF ʲܗ༰ࢺʳ͑ΒΕΔ<ຊޠ8PSE/FU ӳ ՄࢉʀՄ൪<Ϋϩεϥϯήʔδઐޠࣙॻ> $BQBCMFPGCFJOHFOVNFSBUFE DPVOUBCMF<8JLJUJPOBSZ> 15
&OVNFSBCMF ʮFBDIϝιουΛݺͿͱɺԿ͔͕ ॱʹ /ճ ZJFMEͰ͞ΕΔʯ ͱ͍͏ϧʔϧ / 16
&OVNFSBCMF FBDIϝιου͕ఆٛ͞Εͯ ͍ΔΫϥεʹJODMVEFͯ͠ ͏ .JYJO 17
&OVNFSBCMF FBDI͚ͩ༻ҙ͓͚ͯ͠ NBQ TFMFDU SFKFDU HSFQ FUD ͱ͍ͬͨϝιου͕͑ΔΑ͏ʹͳΔ ͜ΕΒͷϝιου͕FBDIΛͬͯ ࣮͞Ε͍ͯΔ
18
"O&YBNQMF DMBTT" JODMVEF&OVNFSBCMF EFGFBDI ZJFME ZJFME ZJFME FOE FOE "OFXNBQ\]J]J
^< > 19
͜͜·Ͱͷ·ͱΊ &OVNFSBCMFFBDIϝιουΛͬͯ ଟ࠼ͳϝιουΛՃ͢Δ ͳʹ͕ʮ্͑͛ΒΕΔʯͷ͔ FBDIϝιου͕ͲͷΑ͏ʹ ZJFMEΛݺͼग़͔͢Ͱܾ·Δ 20
&OVNFSBUPS ͬͯ·͢ 21
&OVNFSBUPS FBDIʹ͢ϒϩοΫΛอཹͨ͠ ঢ়ଶͰΦϒδΣΫτͱͯ࣋ͪ͠ճ͢ 22
UP@FOVN FOVN@GPS &OVNFSBUPSͷ࡞Γ͔ͨ w &OVNFSBUPSOFX PCK NFUIPEFBDI BSHT w
PCKUP@FOVN NFUIPEFBDI BSHT w PCKFOVN@GPS NFUIPEFBDI BSHT w &OVNFSBUPSOFX\]Z]^ ˢ͜Ε͚ͩͪΐͬͱಛघ 23
0CKFDUFOVN@GPS FBDIҎ֎ͷҙͷϝιουͷݺͼग़͠Λ &OVNFSBUPSʹͰ͖Δ ࣮&OVNFSBCMFͰͳ͍͍ͯ͘ F@CZUFTUEJOFOVN@GPS FBDI@CZUF όΠτ͝ͱʹZJFME F@MJOFTUEJOFOVN@GPS FBDI@MJOF ߦ͝ͱʹZJFME
24
&OVNFSBUPSOFX\]Z]^ F&OVNFSBUPSOFX\]Z] Z ZGPP Z ^ ϒϩοΫ͕FBDIͷΘΓɻ ϒϩοΫύϥϝʔλZʹ͢Δͱ ZJFME͢Δ͜ͱʹͳΔɻ 25
&OVNFSBUPSOFX\]Z]^ DMBTT" EFGFBDI ZJFME ZJFMECBS ZJFME FOE FOE "OFXUP@FOVN ͱಉ͡
26
&OVNFSBUPSBT &YUFSOBM*UFSBUPS ֎෦ΠςϨʔλͱͯ͑͠Δ F UP@FOVN FOFYUFBDI͕ZJFME͢Δ͕ FOFYUॱʹOFYUͰऔΓग़ͤΔ FOFYU FOFYU
4UPQ*UFSBUJPOJUFSBUJPOSFBDIFEBOFOE 27
.FUIPE$IBJO NBQ TFMFDUͳͲҰ෦ͷϝιουϒϩο ΫΛলུ͢Δͱ&OVNFSBUPSΛฦ͢ͷͰ ෳͷϝιουΛΈ߹Θͤͯ͏ BSZNBQXJUI@JOEFY\]W J]^ 28
.FUIPE$IBJO <B C D E>NBQXJUI@JOEFYEP]TZN J] JFWFO TZNJ FOE
<B D > &OVNFSBUPS ಠࣗͷϝιου 29
XJUI@JOEFY <SVCZEFW>.BU[XSPUF lNBQ͕FOVNFSBUPSΛฦ͢ͱ PCKNBQXJUI@JOEFY\]Y J]^ ͕Ͱ͖Δͷ͕خ͍͠ͷͰಋೖͨ͠ͷͰͨ͠ɻͱ͍͏Θ͚Ͱɺ FBDIʹNBQʹXJUI@JOEFY͕͚ΒΕͯخ͍͠ ͱ͍͏ͷ͕ຊͷཧ༝Ͱ͢ɻz 30
4FDSFU4UPSZPG &OVNFSBUPS &OVNFSBUPS XJUI@JOEFYͷҝʹ ੜ·Εͨ ͱ͍͏ͷӕͰ͕͢ ओͳ༻్XJUI@JOEFYͷ ͨΊͷΑ͏ͳؾ͕͠·͢ 31
͜͜·Ͱͷ·ͱΊ w&OVNFSBUPS w0CKFDUUP@FOVN FOVN@GPS w֎෦ΠςϨʔλԽ w.FUIPE$IBJO w 32
&OVNFSBUPS-B[Z ͷ৽ػೳ 33
&OVNFSBUPS-B[Z &OVNFSBUPSͷαϒΫϥε 34
&OVNFSBCMFMB[Z &OVNFSBUPS-B[Zͷ࡞Γ͔ͨ &OVNFSBCMFMB[ZΛݺͿ MB[Z &OVNFSBUPS-B[Z 35
&OVNFSBUPS-B[Z -B[ZͷNBQ TFMFDU [JQͳͲҰ෦ͷϝιο υ͕͞Βʹ-B[ZΛฦ͢ MB[ZNBQ\]J]J ^ &OVNFSBUPS-B[Z
&OVNFSBUPS-B[ZNBQ 36
&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)] 37
GPSDF &OVNSBUPS-B[ZGPSDF UP@BͷBMJBT Ԇ͞Ε͍ͯΔΠςϨʔλͷධՁΛ࣮ߦ 38
"-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 ΛݺͿͱ࣮ߦ͞ΕΔ 39
&OVNFSBUPSͱͷҧ͍ .FUIPE$IBJOͷ͍͔͕ͨΑΓڧྗʹ தؒσʔλͷ੍ 40
1JUGBMMPG &OVNFSBUPS NBQ TFMFDUͷΑ͏ʹϒϩοΫͷΓ Λར༻͢ΔϝιουΛෳܨ͛Δ͜ͱ͕Ͱ ͖ͳ͍ ͯ͠ҙຯ͕ͳ͘ͳΔ NBQTFMFDU\]J]J^
< > ˢNBQͷҙຯ͕ͳ͘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GPSDF 52
-B[ZWFSTJPO MBSHF@BSSBZMB[ZNBQ\^TFMFDU\^GPSDF ($`FE 53
-B[ZWFSTJPO MBSHF@BSSBZMB[ZNBQ\^TFMFDU\^GPSDF 54
-B[ZWFSTJPO MBSHF@BSSBZMB[ZNBQ\^TFMFDU\^GPSDF ($`FE 55
-B[ZWFSTJPO MBSHF@BSSBZMB[ZNBQ\^TFMFDU\^GPSDF 56
-B[ZWFSTJPO MBSHF@BSSBZMB[ZNBQ\^TFMFDU\^GPSDF தؒσʔλ͕ෆཁ 57
-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] 58
-B[ZXJUI*0 ྫ TUEJO͔ΒۭߦΛಡΉ·Ͱͷ֤ߦͷ จࣈͷྻΛฦ͢ TUEJOMB[ZUBLF@XIJMF\]M] MDIPNQFNQUZ ^NBQ\]M]MDIPNQTJ[F^ 59
-B[ZXJUI*0 &OVNFSBCMFͩͱۭߦ·ͰઌʹಡΜͰ͠·͏ɻ -B[ZͳΒߦಡΉͨͼʹUBLF@XIJMFNBQͷ ϒϩοΫ͕࣮ߦ͞ΕΔ ˠஞ࣍ॲཧͰ͖Δ 60
-B[Z-JTU ແݶʹଓ͘ཁૉΛѻ͑Δ MJTU 'MPBU*/'*/*5: MJTUNBQ\]J]J ^UBLF ฦͬͯ͜ͳ͍ MJTUMB[ZNBQ\]J]J ^UBLF
GPSDF < > 61
1JUGBMMPG-B[Z ϝϞԽ͞Εͳ͍ GPSDFΛճݺͿͱ͏ҰFBDI͕ ݺΕΔ *0ͳͲ෭࡞༻ͷ͋ΔFBDIͩͱ݁Ռ͕ มԽ͢Δ͚Ͳ͍͍ͷ͔ͳ͊ ͍͍͔ FOVN@GPSͷΑ͏ʹFBDIͷ͔ΘΓʹͳ ΔϝιουΛࢦఆͰ͖ͳ͍ 62
-B[ZBTB#FUUFS &OVNFSBUPS -B[Z &OVNFSBUPSͷ ਖ਼౷ਐԽ൛ 63
)BQQZ-B[Z 1SPHSBNNJOH 64
0OF.PSF5IJOH IUUQTHJUIVCDPNOBHBDIJLBMB[Z@TBNQMF 65