[#13087] importing forwardable — "Akinori MUSHA" <knu@...>

 石塚さんの forwardable.rb を標準添付ライブラリにするべく、

11 messages 2001/05/02

[#13169] SizedQueue#pop causes deadlock — "Okada Jun" <yun@...>

岡田です。

18 messages 2001/05/13
[#13171] Re: SizedQueue#pop causes deadlock — "Akinori MUSHA" <knu@...> 2001/05/13

At Sun, 13 May 2001 14:11:18 +0900,

[#13176] Re: SizedQueue#pop causes deadlock — matz@... (Yukihiro Matsumoto) 2001/05/13

まつもと ゆきひろです

[#13177] Re: SizedQueue#pop causes deadlock — "Akinori MUSHA" <knu@...> 2001/05/13

At Mon, 14 May 2001 00:24:45 +0900,

[#13187] Re: SizedQueue#pop causes deadlock — matz@... (Yukihiro Matsumoto) 2001/05/13

まつもと ゆきひろです

[#13202] Re: [ruby-list:29672] Re: Enumerator — "Akinori MUSHA" <knu@...>

 ruby-dev に移ります。

26 messages 2001/05/15
[#13208] Re: [ruby-list:29672] Re: Enumerator — matz@... (Yukihiro Matsumoto) 2001/05/15

まつもと ゆきひろです

[#13259] Enumerator -- Round 2 — "Akinori MUSHA" <knu@...>

 もう一度、 Enumerable/Enumerator についてみなさんのご意見を

29 messages 2001/05/20
[#13260] Re: Enumerator -- Round 2 — matz@... (Yukihiro Matsumoto) 2001/05/20

まつもと ゆきひろです

[#13265] Re: Enumerator -- Round 2 — "Akinori MUSHA" <knu@...> 2001/05/21

At Mon, 21 May 2001 06:04:32 +0900,

[#13268] Re: Enumerator -- Round 2 — Shin-ichiro HARA <sinara@...> 2001/05/21

原です。

[#13270] Re: Enumerator -- Round 2 — "Akinori MUSHA" <knu@...> 2001/05/21

At Mon, 21 May 2001 15:00:11 +0900,

[#13289] Re: Enumerator -- Round 2 — Shin-ichiro HARA <sinara@...> 2001/05/22

原です。

[#13290] Re: Enumerator -- Round 2 — "Akinori MUSHA" <knu@...> 2001/05/22

At Tue, 22 May 2001 19:02:10 +0900,

[#13291] Re: Enumerator -- Round 2 — Shin-ichiro HARA <sinara@...> 2001/05/22

原です。

[#13293] Re: Enumerator -- Round 2 — "Akinori MUSHA" <knu@...> 2001/05/22

At Tue, 22 May 2001 20:57:02 +0900,

[#13305] Re: Enumerator -- Round 2 — Shin-ichiro HARA <sinara@...> 2001/05/24

原です。

[#13322] Re: Enumerator -- Round 2 — "Akinori MUSHA" <knu@...> 2001/05/24

At Thu, 24 May 2001 15:44:14 +0900,

[#13277] ext/dbm in ruby 1.7 — Kazuhiro NISHIYAMA <zn@...>

ruby 1.7のext/dbmですが、

16 messages 2001/05/21
[#13280] Re: ext/dbm in ruby 1.7 — matz@... (Yukihiro Matsumoto) 2001/05/21

まつもと ゆきひろです

[#13292] Integer("X") rescue -1 が parse error — YASUI Kentarow <kenyasui@...>

安井です。

18 messages 2001/05/22
[#13294] Re: Integer("X") rescue -1 が parse error — matz@... (Yukihiro Matsumoto) 2001/05/22

まつもと ゆきひろです

[#13295] Re: Integer("X") rescue -1 が parse error — "Akinori MUSHA" <knu@...> 2001/05/23

At Wed, 23 May 2001 08:59:50 +0900,

[#13300] 1.6.4 preview3 (Re: Re: Integer("X") rescue -1 が parse error) — matz@... (Yukihiro Matsumoto) 2001/05/24

[#13304] Re: 1.6.4 preview3 (Re: Re: Integer("X") rescue -1 が parse error) — "Akinori MUSHA" <knu@...> 2001/05/24

At Thu, 24 May 2001 14:15:04 +0900,

[#13428] mswin32/ming32 system patch (experimental) — "U.Nakamura" <usa@...>

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

19 messages 2001/05/31
[#13435] Re: mswin32/ming32 system patch (experimental) — nobu.nakada@... 2001/06/01

なかだです。

[#13442] Re: mswin32/ming32 system patch (experimental) — "U.Nakamura" <usa@...> 2001/06/01

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

[#13446] Re: mswin32/ming32 system patch (experimental) — "U.Nakamura" <usa@...> 2001/06/02

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

[#13450] Re: mswin32/ming32 system patch (experimental) — nobu.nakada@... 2001/06/04

なかだです。

[ruby-dev:13259] Enumerator -- Round 2

From: "Akinori MUSHA" <knu@...>
Date: 2001-05-20 19:09:01 UTC
List: ruby-dev #13259
 もう一度、 Enumerable/Enumerator についてみなさんのご意見を
参考に考え直しました。

- each 以外のメソッド、つまり each_byte 等も enumerate したい
  というのが第一の目的。

- Enumerable でないオブジェクトでも、クラスメソッド等に対して
  enumerate したいことがあり、なんとか簡便な手段を提供したい。

- やはり、 each_byte 等を enumerate するときメソッドチェーン
  できないと不便。

- 新設する Enumerator の役割としては、 Enumerable でないものを
  メソッド名(と与える引数)を指定して Enumerable 化するという
  単機能に絞る。

- インデックスを付けたり、複数個ずつ取り出すようにする機能は
  Enumerable に持たせる。すでに each_with_index もあるので、
  その役割を担うのは自然。

- Enumerable に追加するということで、グループ化イテレータの生成
  メソッドの名前は in_groups_of にする。group や group_by では
  ぶつかる危険があるため。

- 一度は Enumerable#enum_with は邪道と思ったが、ニーズを考慮
  すると、 Enumerable なものはやはり優先的に救済されて然るべき。
  Enumerable でないものは、直接 Enumerator[...] を使えばよい。

- couple / pair は見送り。

ということで、以下の新設を再提案します。

----
module Enumerable

  Enumerable#enum_with(method, *args)
	each の代わりに method という名前のメソッドを使って
	繰り返す Enumerator オブジェクトを生成して返す。

	e.g.)
	  s = 'abcdef'
	  a = s.enum_with(:each_byte).collect { |b| "%%%02x" % b }
	  #=> ["%61", "%62", "%63", "%64", "%65", "%66"]

  Enumerable#with_index()
	各要素に 0 から始まるインデックスを付けて繰り返す
	Enumerator オブジェクトを生成して返す。
	(cf. each_with_index)

	e.g.)
	  s = 'abcdef'
	  a = s.enum_with(:each_byte).with_index.collect {
	    |b,i| "#{i}: #{b}"
	  }
	  #=> ["0: 97", "1: 98", "2: 99", "3: 100", "4: 101", "5: 102"]

  Enumerable#in_groups_of(n)
	要素を n 個ずつ取り出し、各配列について繰り返す Enumerator
	オブジェクトを生成して返す。 (cf. each_group_of)

	e.g.)
	  x = [0,100,50,50,100,0]

	  a = x.in_groups_of(2).collect { |x,y| "(#{x},#{y})" }
	  #=> ["(0,100)", "(25,75)", "(50,50)", "(75,25)", "(100,0)"]


  Enumerable#each_group_of(n) { |ary| ... }
	要素を n 個ずつ取り出し、各配列についてブロックを繰り返す。


class Enumerator 
  include Enumerable

  Enumerator[obj, method = :each, *args]
  Enumerator::new(obj, method = :each, *args)
	obj オブジェクトについて、 each の代わりに method という
	名前のメソッドを使って繰り返すオブジェクトを生成して返す。
	args を指定すると、 method の呼び出し時に渡される。

	e.g.)
	  a = Enumerator[ObjectSpace, :each_object, Module].sort {
	    |a,b| a.name <=> b.name
	  }
	  #=> [ArgumentError, Array, ..., ZeroDivisionError, fatal]


  Enumerator#each()
	生成時のパラメータに従って繰り返す。
----

 Ruby による実装を以下に置きました。末尾にも添付します。

	http://www.idaemons.org/~knu/ruby/enumerator.rb


 メソッド名については、後日 RCR して ruby-talk でも英語として
適当かどうか意見を募るつもりです。


 それでは、御評価のほどよろしくお願いします。

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

"Freeze this moment a little bit longer, make each impression
  a little bit stronger..  Experience slips away -- Time stand still"

module Enumerable
  def enum_with(method, *args)
    Enumerator::new(self, method, *args)
  end

  def with_index()
    enum_with(:each_with_index)
  end

  def in_groups_of(n)
    enum_with(:each_group_of, n)
  end

  def each_group_of(n)
    buf = []

    each { |i|
      buf << i
      if buf.size == n
	yield(buf)
	buf.clear
      end
    }

    unless buf.empty?
      yield(buf)
    end
  end
end

class Enumerator
  include Enumerable

  class << self
    alias [] new
  end
  
  def initialize(enumobj, method, *args)
    @enumobj = enumobj
    @method = method
    @args = args
  end
  
  def each(&block)
    @enumobj.send(@method, *@args, &block)
  end
end


if __FILE__ == $0
  a = Enumerator[ObjectSpace, :each_object, Module].sort { |a,b| a.name <=> b.name }

  puts a.join(' ')

  puts '----'

  s = 'abcdef'
  a = s.enum_with(:each_byte).with_index.collect { |b,i| "#{i}: #{b}" }

  puts a

  puts '----'

  x = [0,100,25,75,50,50,75,25,100,0]

  a = x.in_groups_of(2).collect { |x,y| "(#{x},#{y})" }

  puts a
end

In This Thread

Prev Next