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
誰も全体を知らない ~ ロールの垣根を超えて引き上げる開発生産性 / Boosting Development Productivity Across Roles
kakehashi
1
230
Making your applications cross-environment - OSCG 2024 NA
salaboy
0
190
初心者向けAWS Securityの勉強会mini Security-JAWSを9ヶ月ぐらい実施してきての近況
cmusudakeisuke
0
130
OTelCol_TailSampling_and_SpanMetrics
gumamon
1
200
Lexical Analysis
shigashiyama
1
150
データプロダクトの定義からはじめる、データコントラクト駆動なデータ基盤
chanyou0311
2
330
心が動くエンジニアリング ── 私が夢中になる理由
16bitidol
0
100
Can We Measure Developer Productivity?
ewolff
1
150
DynamoDB でスロットリングが発生したとき/when_throttling_occurs_in_dynamodb_short
emiki
0
250
[CV勉強会@関東 ECCV2024 読み会] オンラインマッピング x トラッキング MapTracker: Tracking with Strided Memory Fusion for Consistent Vector HD Mapping (Chen+, ECCV24)
abemii
0
230
飲食店データの分析事例とそれを支えるデータ基盤
kimujun
0
160
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
180
Featured
See All Featured
Teambox: Starting and Learning
jrom
133
8.8k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
Docker and Python
trallard
40
3.1k
The Cult of Friendly URLs
andyhume
78
6k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
900
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Making Projects Easy
brettharned
115
5.9k
Ruby is Unlike a Banana
tanoku
97
11k
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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NBQ\^TFMFDU\^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