[#18651] Enumerable#zip — matz@... (Yukihiro Matsumoto)

まつもと ゆきひろです

77 messages 2002/11/05
[#18659] Re: Enumerable#zip — Shin-ichiro HARA <sinara@...> 2002/11/06

原です。

[#18669] Re: Enumerable#zip — matz@... (Yukihiro Matsumoto) 2002/11/06

まつもと ゆきひろです

[#18675] Re: Enumerable#zip — Shin-ichiro HARA <sinara@...> 2002/11/07

原です。

[#18684] Re: Enumerable#zip — matz@... (Yukihiro Matsumoto) 2002/11/07

まつもと ゆきひろです

[#18690] Re: Enumerable#zip — Shin-ichiro HARA <sinara@...> 2002/11/08

原です。

[#18728] Re: Enumerable#zip — Daisuke Aoki <dai@...> 2002/11/09

青木@横浜です。基本的にROM な人間なんですがなんとなく書いてみました。

[#18774] Re: Enumerable#zip — Daisuke Aoki <dai@...> 2002/11/13

青木@横浜です。

[#18799] Re: Enumerable#zip — Shin-ichiro HARA <sinara@...> 2002/11/15

原です。

[#18802] Re: Enumerable#zip — Tanaka Akira <akr@...17n.org> 2002/11/15

In article <5.1.1.8.2.20021115145423.03541008@blade.nagaokaut.ac.jp>,

[#18803] Re: Enumerable#zip — matz@... (Yukihiro Matsumoto) 2002/11/15

まつもと ゆきひろです

[#18806] Re: Enumerable#zip — Tanaka Akira <akr@...17n.org> 2002/11/15

In article <1037348006.479453.32695.nullmailer@picachu.netlab.jp>,

[#18808] Re: Enumerable#zip — matz@... (Yukihiro Matsumoto) 2002/11/15

まつもと ゆきひろです

[#18823] Re: Enumerable#zip — Shugo Maeda <shugo@...> 2002/11/18

前田です。

[#18833] Re: Enumerable#zip — Tanaka Akira <akr@...17n.org> 2002/11/18

In article <8765uv4fu7.wl@studly.priv.netlab.jp>,

[#18835] Re: Enumerable#zip — Shugo Maeda <shugo@...> 2002/11/18

前田です。

[#18845] Re: Enumerable#zip — Tanaka Akira <akr@...17n.org> 2002/11/19

In article <874raf6xuc.wl@studly.priv.netlab.jp>,

[#18870] Re: Enumerable#zip — Shugo Maeda <shugo@...> 2002/11/21

前田です。

[#18873] Re: Enumerable#zip — Tanaka Akira <akr@...17n.org> 2002/11/21

In article <87fztv5zir.wl@studly.priv.netlab.jp>,

[#18692] Re: Enumerable#zip — Tanaka Akira <akr@...17n.org> 2002/11/08

In article <4.3.2-J.20021108124934.033a2eb0@blade.nagaokaut.ac.jp>,

[#18696] Re: Enumerable#zip — Shin-ichiro HARA <sinara@...> 2002/11/08

原です。

[#18713] Re: Enumerable#zip — matz@... (Yukihiro Matsumoto) 2002/11/09

まつもと ゆきひろです

[#18719] Re: Enumerable#zip — Tanaka Akira <akr@...17n.org> 2002/11/09

In article <1036742681.769607.13070.nullmailer@picachu.netlab.jp>,

[#18721] Re: Enumerable#zip — matz@... (Yukihiro Matsumoto) 2002/11/09

まつもと ゆきひろです

[#18723] Re: Enumerable#zip — Tanaka Akira <akr@...17n.org> 2002/11/09

In article <1036847474.074389.7942.nullmailer@picachu.netlab.jp>,

[#18735] Re: Enumerable#zip — Shin-ichiro HARA <sinara@...> 2002/11/11

原です。

[#18746] Re: Enumerable#zip — matz@... (Yukihiro Matsumoto) 2002/11/11

まつもと ゆきひろです

[#18749] Re: Enumerable#zip — Shin-ichiro HARA <sinara@...> 2002/11/12

原です。

[#18766] Re: Enumerable#zip — matz@... (Yukihiro Matsumoto) 2002/11/13

まつもと ゆきひろです

[#18800] Re: Enumerable#zip — Shin-ichiro HARA <sinara@...> 2002/11/15

原です。

[#18801] Re: Enumerable#zip — matz@... (Yukihiro Matsumoto) 2002/11/15

まつもと ゆきひろです

[#18804] Re: Enumerable#zip — Shin-ichiro HARA <sinara@...> 2002/11/15

原です。

[#18805] Re: Enumerable#zip — matz@... (Yukihiro Matsumoto) 2002/11/15

まつもと ゆきひろです

[#18754] Different caller(0) in trace_func when NameError from toplevel between 1.6 and 1.7 — "NAKAMURA, Hiroshi" <nahi@...>

なひです。

27 messages 2002/11/12
[#18755] Re: Different caller(0) in trace_func when NameError from toplevel between 1.6 and 1.7 — nobu.nakada@... 2002/11/12

なかだです。

[#18776] Re: Different caller(0) in trace_func when NameError from toplevel between 1.6 and 1.7 — "NAKAMURA, Hiroshi" <nakahiro@...> 2002/11/14

なひです。

[#18777] Re: Different caller(0) in trace_func when NameError from toplevel between 1.6 and 1.7 — "NAKAMURA, Hiroshi" <nakahiro@...> 2002/11/14

なひです。ひええすいません、タブが落ちちゃった。

[#18789] Re: Different caller(0) in trace_func when NameError from toplevel between 1.6 and 1.7 — "NAKAMURA, Hiroshi" <nahi@...> 2002/11/14

なひです。

[#18795] Re: Different caller(0) in trace_func when NameError from toplevel between 1.6 and 1.7 — nobu.nakada@... 2002/11/15

なかだです。

[#18820] deprecated method( Re: Different caller(0) in trace_func when NameError from toplevel between 1.6 and 1.7) — nobu.nakada@... 2002/11/18

なかだです。

[#18821] Re: deprecated method( Re: Different caller(0) in trace_func when NameError from toplevel between 1.6 and 1.7) — matz@... (Yukihiro Matsumoto) 2002/11/18

まつもと ゆきひろです

[#18825] Re: deprecated method( Re: Different caller(0) in trace_func when NameError from toplevel between 1.6 and 1.7) — nobu.nakada@... 2002/11/18

なかだです。

[#18861] class variables — matz@... (Yukihiro Matsumoto)

まつもと ゆきひろです

31 messages 2002/11/20
[#18913] Re: class variables — "K.Kosako" <kosako@...> 2002/11/22

Yukihiro Matsumotoさんの

[#18895] [patch] install bat file on Windows — "U.Nakamura" <usa@...>

こんにちは、なかむら(う)です。

14 messages 2002/11/22
[#18900] Re: [patch] install bat file on Windows — "Akinori MUSHA" <knu@...> 2002/11/22

At Fri, 22 Nov 2002 11:09:19 +0900,

[#18903] Re: [patch] install bat file on Windows — "U.Nakamura" <usa@...> 2002/11/22

こんにちは、なかむら(う)です。

[#18966] [ONIGURUMA] \S doesn't match multibyte char (Re: [ruby-list:36618] Re: 全角文字に挟まれた半角スペースを削除するには?) — nobu.nakada@...

なかだです。

13 messages 2002/11/28

[ruby-dev:18747] Re: Enumerable#zip

From: "Akinori MUSHA" <knu@...>
Date: 2002-11-11 15:54:33 UTC
List: ruby-dev #18747
At Tue, 12 Nov 2002 00:25:34 +0900,
matz wrote:
> cについては、
> 
>   class IndexedEnumerable
>     include Enumerable
>     def initialize(e)
>       @enum = e
>       @index = 0
>     end
>     def each
>       @enum.each do |x|
>         yield x, @index
>         @index += 1
>       end
>     end
>   end
>   b = IndexedEnumerable.new(a)
>   b.map{|x,i|...}
> 
> のようなのがあったら問題解決?

 これはすなわち

class IndexedEnumerable
  include Enumerable
  def initialize(e)
    @enum = e
  end
  def each
    @enum.each_with_index do |x|
      yield x
   end
  end
end

であり、 each_with_index を each とするというものですが、より
一般化したやつを前に書いたことがあります。(提案もしたっけ)

http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/rough/ext/enumerator/

これを使うと

a.enum_with_index.map{|x,i|...}

または

b = Enumerator.new(a, :each_with_index)
b.map{|x,i|...}

となります。


 あと、こないだ発作的に作ってみたのが、指定したクラスの each_foo
というメソッドすべて、もしくは指定したイテレータメソッド foo に
ついて map_foo, grep_foo, ... を定義してしまうという強引な物です。

module Enumerable
  class Enumerator
    include Enumerable

    def initialize(enumobj, method, *args)
      @enumobj = enumobj
      @method = method
      @args = args
    end
    
    def each(&block)
      @enumobj.send(@method, *@args, &block)
    end
  end

  def Enumerable.activate_enum_sub(enum_method)
    enum_method = enum_method.to_s if !enum_method.is_a?(String)

    if /^each_(.*)/ =~ enum_method
      suffix = $1
    else
      suffix = enum_method
    end

    Enumerable.instance_methods.each { |meth|
      if /([?!])$/ =~ meth
	suffixed_meth = sprintf('%s_%s%c', meth[0..-2], suffix, meth[-1])
      else
	suffixed_meth = sprintf('%s_%s', meth, suffix)
      end
      
      yield <<-EOF
	def #{suffixed_meth}(*args, &block)
	  Enumerator.new(self, :#{enum_method}).#{meth}(*args, &block)
        end
      EOF
    }
  end

  def Enumerable.activate_module_enum(mod, *enum_methods)
    enum_methods.concat(mod.instance_methods.grep(/^each_[a-z_]+/))

    enum_methods.each { |enum_method|
      activate_enum_sub(enum_method) { |method_def|
	mod.module_eval method_def
      }
    }
  end

  def Enumerable.activate_singleton_enum(obj, *enum_methods)
    enum_methods.concat(obj.singleton_methods.grep(/^each_[a-z_]+/))

    enum_methods.each { |enum_method|
      activate_enum_sub(enum_method) { |method_def|
	obj.instance_eval method_def
      }
    }
  end
end


Enumerable.activate_module_enum Hash

a = { 1=>'a', 2=>'bb', 3=>'ccc', 0=>'abc' }

p a.grep_value(/b/)
p (a.select_value {|v| v.size == 3 })
p a.max_key

Enumerable.activate_singleton_enum ObjectSpace

p ObjectSpace.grep_object(Module) 

require 'etc'

Enumerable.activate_singleton_enum Etc, :group, :passwd

p (Etc.select_passwd { |u| u.uid < 10 })

p (Etc.collect_group { |g| g.name })


 便利そうなんだけど、標準に持っていくにはいまひとつケレン味が
抜けない感じもする。かといって標準になければ使えないのと同じと
いうのは前にも言った通り。

 イテレータについては Ruby の大きな魅力の部分でもあるので、
いろいろな人の意見を聞きたいところです。

-- 
                     /
                    /__  __            Akinori.org / MUSHA.org
                   / )  )  ) )  /     FreeBSD.org / Ruby-lang.org
Akinori MUSHA aka / (_ /  ( (__(  @ iDaemons.org / and.or.jp

"When I leave I don't know what I'm hoping to find
              When I leave I don't know what I'm leaving behind.."

In This Thread