Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

nagachika
December 02, 2012

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

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

nagachika

December 02, 2012
Tweet

More Decks by nagachika

Other Decks in Programming

Transcript

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

    .PEVMFQSFQFOE͸$ͷϝιουΛ্ॻ͖Ͱ͖Δ "SPVOE"MJBT BMJBT@NFUIPE@DIBJO ͷ͔ΘΓʹ࢖͑Δ C M Object obj
  2. .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
  3. .PEVMFQSFQFOE QSFQFOEͳͩ͠ͱ JODMVEFͩͱ DMBTT1FSTPO  FOE NPEVMF5JUMF  FOE DMBTT.FO1FSTPO

    JODMVEF5JUMF EFGJOJUJBMJ[F TVQFS !UJUMFl.Sz FOE FOE
  4. UP@FOVN FOVN@GPS &OVNFSBUPSͷ࡞Γ͔ͨ w &OVNFSBUPSOFX PCK NFUIPEFBDI  BSHT w

    PCKUP@FOVN NFUIPEFBDI  BSHT w PCKFOVN@GPS NFUIPEFBDI  BSHT w &OVNFSBUPSOFX\]Z]^ ˢ͜Ε͚ͩͪΐͬͱಛघ
  5. &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)]
  6. "-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 ΛݺͿͱ࣮ߦ͞ΕΔ
  7. -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]
  8. 1JUGBMMPG-B[Z ϝϞԽ͞Εͳ͍ GPSDFΛճݺͿͱ΋͏Ұ౓FBDI͕ ݺ͹ΕΔ *0ͳͲ෭࡞༻ͷ͋ΔFBDIͩͱ݁Ռ͕ มԽ͢Δ FY TUEJOMB[ZUBLF  

    FOVN@GPSͷΑ͏ʹFBDIͷ͔ΘΓʹͳ ΔϝιουΛࢦఆͰ͖ͳ͍ TUEJOFOVN@GPS FBDI@CZUF MB[Z TUEJOMB[Z FBDI@CZUF ͱॻ͖͍ͨ