怠惰なRubyistへの道 - Enumerator::Lazy の使いかた@福岡Ruby会議01

5cf8f058a4c094bb708174fb43e7a387?s=47 nagachika
December 02, 2012

怠惰なRubyistへの道 - Enumerator::Lazy の使いかた@福岡Ruby会議01

福岡Ruby会議01での発表資料を公開用にPDFに変換したものです。アニメーションやデモの動画はスライドによる説明に置き換えています。

5cf8f058a4c094bb708174fb43e7a387?s=128

nagachika

December 02, 2012
Tweet

Transcript

  1. ଵଦͳ3VCZJTU΁ͷಓ &OVNFSBUPS-B[Zͷ࢖͍͔ͨ $IJLBOBHB5PNPZVLJ !OBHBDIJLB 'VLVPLBSC 'VLVPLB3VCZ,BJHJ

  2. "HFOEB &OVNFSBCMF &OVNFSBUPS &OVNFSBUPS-B[Z

  3. "HFOEB &OVNFSBCMF ͷ৽ػೳ /&8 &OVNFSBUPS &OVNFSBUPS-B[Z

  4. "55&/5*0/ ࠓ೔ͷ࿩͸ 3VCZͷ ৽ػೳʹ͍ͭͯ

  5. 3VCZ ೥ϦϦʔε༧ఆ <SVCZEFW>ΑΓ "VHCJHGFBUVSFGSFF[F 0DUGFBUVSFGSFF[F 'FCSFMFBTF 3VCZͷੜ஀प೥

  6. 3VCZ "VHCJHGFBUVSFGSFF[F 0DUGFBUVSFGSFF[F /PWQSFWJFXSFMFBTFE ˡࠓίί %FDQSFWJFXSFMFBTFE 'FCSFMFBTF

  7. JTJO EFWFMPQNFOU ˞஫˞ ࢓༷͸։ൃதͷ΋ͷͰ͋Γ ϦϦʔε൛Ͱ͸มߋ͞ΕΔ৔߹͕͋Γ·͢

  8. *OTUBMM3VCZ ͰະདྷΛ֞ؒݟΔ SWN SCFOWΛ࢖͍ͬͯΕ͹ SWNJOTUBMMSVCZIFBE SCFOWJOTUBMMEFWFM

  9. 4FMG*OUSPEVDUJPO UXJUUFSJE!OBHBDIJLB SVCZUSVOLDIBOHFT IUUQEIBUFOBOFKQOBHBDIJLB $3VCZDPNNJUUFS :PLPIBNBSCˠ'VLVPLBSC 4PVOESC SVCZDPSFBVEJP SVCZQVSFEBUB

  10. 3FDFOU"DUJWJUZ 3%PDˠ 3VCZ(FNTˠ 3BLFˠ 3BLF .JOJ5FTUˠ ۭલͷϝδϟʔόʔδϣϯΞοϓϥογϡ

  11. 3FDFOU"DUJWJUZ ͔͚͜Έίϛοτϥογϡ

  12. /FX'FBUVSFT ΩʔϫʔυҾ਺ .PEVMFQSFQFOE 3FGJOFNFOU &OVNFSBUPS-B[Z %5SBDFରԠ 5SBDF1PJOU "SSBZCTFBSDI ($CJUNBQNBSLJOH %FCVHHFS1SPGJMFS

    "1* εΫϦϓτΤϯίʔσΟϯά 65'Խ 4USJOHC
  13. ,FZXPSE"SHVNFOUT EFGUVQQMF LFZlBz WBMVFlCz \LFZWBMVF^ FOE UVQQMFLFZlLz WBMVFlWz \lLzzWz^ UVQQMFWBMVF\lBz^

    UVQQMF\lBzlCz^
  14. .PEVMFQSFQFOE DMBTT$JODMVEF.FOE .PEVMFJODMVEF .JYJO ͸ఆٛࡁΈͷϝιουΛ্ॻ͖Ͱ͖ͳ͍ C M Object obj DMBTT$QSFQFOE.FOE

    .PEVMFQSFQFOE͸$ͷϝιουΛ্ॻ͖Ͱ͖Δ "SPVOE"MJBT BMJBT@NFUIPE@DIBJO ͷ͔ΘΓʹ࢖͑Δ C M Object obj
  15. .PEVMFQSFQFOE NPEVMF5JUMF EFGJOJUJBMJ[F UJUMF OBNF !UJUMFUJUMF TVQFS OBNF FOE EFGOBNF

    !UJUMF TVQFS FOE FOE DMBTT1FSTPO EFGJOJUJBMJ[F OBNF !OBNFOBNFFOE EFGOBNF !OBNF FOE QSFQFOE5JUMF FOE 1FSTPOOFX l.Sz lOBHBDIJLBz OBNFl.SOBHBDIJLBz
  16. .PEVMFQSFQFOE QSFQFOEͳͩ͠ͱ JODMVEFͩͱ DMBTT1FSTPO  FOE NPEVMF5JUMF  FOE DMBTT.FO1FSTPO

    JODMVEF5JUMF EFGJOJUJBMJ[F TVQFS !UJUMFl.Sz FOE FOE
  17. 3FGJOFNFOU େ෯ ࿏ઢ มߋ

  18. !B@NBUTVEB

  19. &OVNFSBUPS-B[Z

  20. &OVNFSBUPS-B[Z ͷલʹ

  21. &OVNFSBCMF

  22. &OVNFSBCMF ΋ͪΖΜ࢖ͬͯ·͢ΑͶ

  23. "SSBZ )BTI *0 "SSBZ΍)BTI *0͸ &OVNFSBCMFΛ JODMVEF͍ͯ͠Δ

  24. &OVNFSBCMF ʲܗ༰ࢺʳ਺͑ΒΕΔ<೔ຊޠ8PSE/FU ӳ࿨ ՄࢉʀՄ෇൪<Ϋϩεϥϯήʔδ෼໺ઐ໳ޠࣙॻ> $BQBCMFPGCFJOHFOVNFSBUFE DPVOUBCMF<8JLJUJPOBSZ>

  25. &OVNFSBCMF ʮFBDIϝιουΛݺͿͱɺԿ͔͕ ॱʹ /ճ ZJFMEͰ౉͞ΕΔʯ ͱ͍͏ϧʔϧ /

  26. &OVNFSBCMF FBDIϝιου͕ఆٛ͞Εͯ ͍ΔΫϥεʹJODMVEFͯ͠ ࢖͏ .JYJO

  27. &OVNFSBCMF FBDI͚ͩ༻ҙ͓͚ͯ͠͹ NBQ TFMFDU SFKFDU HSFQ FUD ͱ͍ͬͨϝιου͕࢖͑ΔΑ͏ʹͳΔ ͜ΕΒͷϝιου͕FBDIΛ࢖ͬͯ ࣮૷͞Ε͍ͯΔ

  28. "O&YBNQMF DMBTT" JODMVEF&OVNFSBCMF EFGFBDI ZJFME ZJFME ZJFME FOE FOE "OFXNBQ\]J]J

    ^<  >
  29. ͜͜·Ͱͷ·ͱΊ &OVNFSBCMF͸FBDIϝιουΛ࢖ͬͯ ଟ࠼ͳϝιουΛ௥Ճ͢Δ ͳʹ͕ʮ਺্͑͛ΒΕΔʯͷ͔͸ FBDIϝιου͕ͲͷΑ͏ʹ ZJFMEΛݺͼग़͔͢Ͱܾ·Δ

  30. &OVNFSBUPS &OVNFSBUPS ࢖ͬͯ·͢

  31. &OVNFSBUPS FBDIʹ౉͢ϒϩο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UPS ಠࣗͷϝιου
  39. XJUI@JOEFY <SVCZEFW>.BU[XSPUF lNBQ͕FOVNFSBUPSΛฦ͢ͱ PCKNBQXJUI@JOEFY\]Y J]^ ͕Ͱ͖Δͷ͕خ͍͠ͷͰಋೖͨ͠ͷͰͨ͠ɻͱ͍͏Θ͚Ͱɺ  FBDIʹ΋NBQʹ΋XJUI@JOEFY͕෇͚ΒΕͯخ͍͠ ͱ͍͏ͷ͕ຊ౰ͷཧ༝Ͱ͢ɻz

  40. 4FDSFU4UPSZPG &OVNFSBUPS &OVNFSBUPS͸ XJUI@JOEFYͷҝʹ ੜ·Εͨ

  41. ͜͜·Ͱͷ·ͱΊ w&OVNFSBUPS w֎෦ΠςϨʔλԽ wϝιουνΣʔϯ w

  42. &OVNFSBUPS-B[Z ͷ৽ػೳ

  43. &OVNFSBUPS-B[Z &OVNFSBUPSͷαϒΫϥε

  44. &OVNFSBCMFMB[Z &OVNFSBUPS-B[Zͷ࡞Γ͔ͨ &OVNFSBCMFMB[ZΛݺͿ   MB[Z &OVNFSBUPS-B[Z

  45. &OVNFSBUPS-B[Z -B[ZͷNBQ TFMFDU [JQͳͲҰ෦ͷϝιο υ͕͞Βʹ-B[ZΛฦ͢   MB[ZNBQ\]J]J ^ &OVNFSBUPS-B[Z

    &OVNFSBUPS-B[ZNBQ
  46. &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)]
  47. GPSDF &OVNSBUPS-B[ZGPSDF UP@BͷBMJBT ஗Ԇ͞Ε͍ͯΔΠςϨʔλͷධՁΛ࣮ߦ

  48. "-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 ΛݺͿͱ࣮ߦ͞ΕΔ
  49. &OVNFSBUPSͱͷҧ͍ .FUIPE$IBJOͷ࢖͍͔͕ͨΑΓڧྗʹ தؒσʔλͷ཈੍

  50. 1JUGBMMPG &OVNFSBUPS  NBQ TFMFDUͷΑ͏ʹϒϩοΫͷ໭Γ஋ Λར༻͢ΔϝιουΛෳ਺ܨ͛Δ͜ͱ͕Ͱ ͖ͳ͍ ͯ͠΋ҙຯ͕ͳ͘ͳΔ  NBQTFMFDU\]J]J^

    <  > ˢNBQͷҙຯ͕ͳ͘ͳ͍ͬͯΔ
  51. 5SVF.FUIPE$IBJO NBQ΍TFMFDUͱ͍ͬͨϝιου΋͍ͭ͘Ͱ΋ .FUIPE$IBJOͰ͖Δɻ ͦ͏ɺ-B[ZͳΒͶɻ ͔Βͷ͏ͪ೚ҙͷܻʹΛؚΉ਺Λฦ͢  MB[ZNBQ UP@T TFMFDU\]T] _T

    ^GPSDF
  52. 1JUGBMMPG &OVNFSBUPS  NBQ TFMFDUͷΑ͏ʹϒϩοΫͷ໭Γ஋ Λར༻͢ΔϝιουΛ֎෦ΠςϨʔλԽ͠ ͯ΋ҙຯ͕ͳ͍ F  NBQ

    FOFYU FOFYU FOFYU FOFYU ˢNBQͷҙຯ͕ͳ͘ͳ͍ͬͯΔ
  53. 5SVF&YUFSOBM *UFSBUPS NBQͳͲʹϒϩοΫΛ౉ͤΔͷͰ ద༻ͨ݁͠ՌΛॱʹऔΓग़ͤΔ M  MB[ZTFMFDU\]J]JFWFO ^ MOFYU MOFYU

    MOFYU
  54. &GGJDJFOU.FNPSZ 6TBHF -B[Z͸ZJFMEຖʹ.FUIPE$IBJOͷ ࠷ޙ·Ͱॲཧ͞ΕΔ

  55. &OVNFSBCMFWFSTJPO MBSHF@BSSBZNBQ\^TFMFDU\^

  56. &OVNFSBCMFWFSTJPO MBSHF@BSSBZNBQ\^TFMFDU\^ NBQ

  57. &OVNFSBCMFWFSTJPO MBSHF@BSSBZNBQ\^TFMFDU\^ NBQ TFMFDU

  58. &OVNFSBCMFWFSTJPO MBSHF@BSSBZNBQ\^TFMFDU\^ NBQ TFMFDU ($͞ΕΔ

  59. &OVNFSBCMFWFSTJPO MBSHF@BSSBZNBQ\^TFMFDU\^ NBQ TFMFDU ($͞ΕΔ map ݁ՌͷͨΊ large_array ͱಉ͘͡Β͍ͷ தؒσʔλ͕ඞཁ

  60. -B[ZWFSTJPO MBSHF@BSSBZMB[ZNBQ\^TFMFDU\^GPSDF

  61. -B[ZWFSTJPO MBSHF@BSSBZMB[ZNBQ\^TFMFDU\^GPSDF

  62. -B[ZWFSTJPO MBSHF@BSSBZMB[ZNBQ\^TFMFDU\^GPSDF ($`FE

  63. -B[ZWFSTJPO MBSHF@BSSBZMB[ZNBQ\^TFMFDU\^GPSDF

  64. -B[ZWFSTJPO MBSHF@BSSBZMB[ZNBQ\^TFMFDU\^GPSDF ($`FE

  65. -B[ZWFSTJPO MBSHF@BSSBZMB[ZNBQ\^TFMFDU\^GPSDF

  66. -B[ZWFSTJPO MBSHF@BSSBZMB[ZNBQ\^TFMFDU\^GPSDF தؒσʔλ͕ෆཁ

  67. -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]
  68. -B[ZXJUI*0 ྫ TUEJO͔ΒۭߦΛಡΉ·Ͱͷ֤ߦͷ จࣈ਺ͷ഑ྻΛฦ͢ TUEJOMB[ZUBLF@XIJMF\]M] MDIPNQFNQUZ ^NBQ\]M]MDIPNQTJ[F^

  69. -B[ZXJUI*0 &OVNFSBCMF &OVNFSBUPSͩͱۭߦ·Ͱઌ ʹಡΜͰ͠·͏ɻ -B[ZͳΒߦಡΉͨͼʹUBLF@XIJMFNBQ ͷϒϩοΫ͕࣮ߦ͞ΕΔ ˠஞ࣍ॲཧͰ͖Δ

  70. -B[Z-JTU ແݶʹଓ͘ཁૉΛѻ͑Δ MJTU 'MPBU*/'*/*5: MJTUNBQ\]J]J ^UBLF  ฦͬͯ͜ͳ͍ MJTUMB[ZNBQ\]J]J ^UBLF

     GPSDF <    >
  71. *OUFSBDUJWJUZ ࣮ߦॱংΛར༻ͯ͠ΠϯλϥΫςΟϒʹ 'MPBU*/'*/*5: MB[ZNBQ\]J] QOVN MJOFTUEJOHFUT MJOFDIPNQJGMJOF ^UBLF@XIJMF\]M] MBOEOPUMFNQUZ ^GPSDF

  72. 1JUGBMMPG-B[Z ϝϞԽ͞Εͳ͍ GPSDFΛճݺͿͱ΋͏Ұ౓FBDI͕ ݺ͹ΕΔ *0ͳͲ෭࡞༻ͷ͋ΔFBDIͩͱ݁Ռ͕ มԽ͢Δ FY TUEJOMB[ZUBLF  

    FOVN@GPSͷΑ͏ʹFBDIͷ͔ΘΓʹͳ ΔϝιουΛࢦఆͰ͖ͳ͍ TUEJOFOVN@GPS FBDI@CZUF MB[Z TUEJOMB[Z FBDI@CZUF ͱॻ͖͍ͨ
  73. -B[ZBTB#FUUFS &OVNFSBUPS -B[Z͸ &OVNFSBUPSͷ ਖ਼౷ਐԽ൛

  74. )BQQZ-B[Z 1SPHSBNNJOH