怠惰なRubyistへの道 - Enumerator::Lazy の使いかた

怠惰なRubyistへの道 - Enumerator::Lazy の使いかた

みなとRuby会議01での発表「怠惰なRubyistへの道 - Enumerator::Lazy の使いかた」の発表資料を配布用に編集したものです。

5cf8f058a4c094bb708174fb43e7a387?s=128

nagachika

June 02, 2012
Tweet

Transcript

  1. ଵଦͳ3VCZJTU΁ͷಓ &OVNFSBUPS-B[Zͷ࢖͍͔ͨ $IJLBOBHB5PNPZVLJ !OBHBDIJLB :PLPIBNBSC 1

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

  3. 8"3/*/( ࠓ೔ͷ࿩͸ 3VCZͷ ৽ػೳʹ͍ͭͯ 3

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

  5. 3VCZ ະདྷͷ࿩ 5

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

  7. *OTUBMM3VCZ ະདྷΛखதʹ SWN SCFOWΛ࢖͍ͬͯΕ͹ SWNJOTUBMMSVCZIFBE SCFOWJOTUBMMEFWFM 7

  8. 3VCZGPS 8JOEPXT 8

  9. 4FMG*OUSPEVDUJPO UXJUUFSJE!OBHBDIJLB SVCZUSVOLDIBOHFT IUUQEIBUFOBOFKQOBHBDIJLB $3VCZDPNNJUUFS :PLPIBNBSCˠ෱Ԭԕ੐த 4PVOESC SVCZDPSFBVEJP SVCZQVSFEBUB 9

  10. &OVNFSBUPS-B[Z 10

  11. &OVNFSBUPS-B[Z ͷલʹ 11

  12. &OVNFSBCMF 12

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

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

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

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

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

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

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

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

  21. &OVNFSBUPS ࢖ͬͯ·͢ 21

  22. &OVNFSBUPS FBDIʹ౉͢ϒϩοΫΛอཹͨ͠ ঢ়ଶͰΦϒδΣΫτͱͯ࣋ͪ͠ճ͢ 22

  23. UP@FOVN FOVN@GPS &OVNFSBUPSͷ࡞Γ͔ͨ w &OVNFSBUPSOFX PCK NFUIPEFBDI  BSHT w

    PCKUP@FOVN NFUIPEFBDI  BSHT w PCKFOVN@GPS NFUIPEFBDI  BSHT w &OVNFSBUPSOFX\]Z]^ ˢ͜Ε͚ͩͪΐͬͱಛघ 23
  24. 0CKFDUFOVN@GPS FBDIҎ֎ͷ೚ҙͷϝιουͷݺͼग़͠Λ &OVNFSBUPSʹͰ͖Δ ࣮͸&OVNFSBCMFͰͳͯ͘΋͍͍ F@CZUFTUEJOFOVN@GPS FBDI@CZUF όΠτ͝ͱʹZJFME F@MJOFTUEJOFOVN@GPS FBDI@MJOF ߦ͝ͱʹZJFME

    24
  25. &OVNFSBUPSOFX\]Z]^ F&OVNFSBUPSOFX\]Z] Z ZGPP Z ^ ϒϩοΫ͕FBDIͷ୅ΘΓɻ ϒϩοΫύϥϝʔλZʹ͢Δͱ ZJFME͢Δ͜ͱʹͳΔɻ 25

  26. &OVNFSBUPSOFX\]Z]^ DMBTT" EFGFBDI ZJFME ZJFMECBS ZJFME FOE FOE "OFXUP@FOVN ͱಉ͡

    26
  27. &OVNFSBUPSBT &YUFSOBM*UFSBUPS ֎෦ΠςϨʔλͱͯ͠࢖͑Δ F  UP@FOVN FOFYUFBDI͕ZJFME͢Δ஋͕ FOFYUॱʹOFYUͰऔΓग़ͤΔ FOFYU FOFYU

    4UPQ*UFSBUJPOJUFSBUJPOSFBDIFEBOFOE 27
  28. .FUIPE$IBJO NBQ TFMFDUͳͲҰ෦ͷϝιου͸ϒϩο ΫΛলུ͢Δͱ&OVNFSBUPSΛฦ͢ͷͰ ෳ਺ͷϝιουΛ૊Έ߹Θͤͯ࢖͏ BSZNBQXJUI@JOEFY\]W J]^ 28

  29. .FUIPE$IBJO <B C D E>NBQXJUI@JOEFYEP]TZN J] JFWFO  TZNJ FOE

    <B  D > &OVNFSBUPS ಠࣗͷϝιου 29
  30. XJUI@JOEFY <SVCZEFW>.BU[XSPUF lNBQ͕FOVNFSBUPSΛฦ͢ͱ PCKNBQXJUI@JOEFY\]Y J]^ ͕Ͱ͖Δͷ͕خ͍͠ͷͰಋೖͨ͠ͷͰͨ͠ɻͱ͍͏Θ͚Ͱɺ  FBDIʹ΋NBQʹ΋XJUI@JOEFY͕෇͚ΒΕͯخ͍͠ ͱ͍͏ͷ͕ຊ౰ͷཧ༝Ͱ͢ɻz 30

  31. 4FDSFU4UPSZPG &OVNFSBUPS &OVNFSBUPS͸ XJUI@JOEFYͷҝʹ ੜ·Εͨ ͱ͍͏ͷ͸ӕͰ͕͢ ओͳ༻్͸XJUI@JOEFYͷ ͨΊͷΑ͏ͳؾ͕͠·͢ 31

  32. ͜͜·Ͱͷ·ͱΊ w&OVNFSBUPS w0CKFDUUP@FOVN FOVN@GPS w֎෦ΠςϨʔλԽ w.FUIPE$IBJO w 32

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

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

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

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

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

  39. "-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
  40. &OVNFSBUPSͱͷҧ͍ .FUIPE$IBJOͷ࢖͍͔͕ͨΑΓڧྗʹ தؒσʔλͷ཈੍ 40

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

     GPSDF <    > 61
  62. 1JUGBMMPG-B[Z ϝϞԽ͞Εͳ͍ GPSDFΛճݺͿͱ΋͏Ұ౓FBDI͕ ݺ͹ΕΔ *0ͳͲ෭࡞༻ͷ͋ΔFBDIͩͱ݁Ռ͕ มԽ͢Δ͚Ͳ͍͍ͷ͔ͳ͊ ͍͍͔΋ FOVN@GPSͷΑ͏ʹFBDIͷ͔ΘΓʹͳ ΔϝιουΛࢦఆͰ͖ͳ͍ 62

  63. -B[ZBTB#FUUFS &OVNFSBUPS -B[Z͸ &OVNFSBUPSͷ ਖ਼౷ਐԽ൛ 63

  64. )BQQZ-B[Z 1SPHSBNNJOH 64

  65. 0OF.PSF5IJOH IUUQTHJUIVCDPNOBHBDIJLBMB[Z@TBNQMF 65