Project

General

Profile

Actions

Feature #2673

closed

the length for an enumerator generated by Array#permutation and Array#combination

Added by mrkn (Kenta Murata) about 14 years ago. Updated over 6 years ago.

Status:
Closed
Target version:
[ruby-dev:40200]

Description

=begin
Array#permutation と Array#combination が生成する enumerator は要素数が確定できますが、
Enumerator#length が存在しないため、to_a で配列化しなければ長さを取得できません。

この欠点を解消するため、Array#permutation と Array#combination が生成する enumerator に対して
length メソッドを特異メソッドとして追加するパッチを書きました。
差し支えないようでしたら trunk への取り込みを検討し頂けませんでしょうか。

よろしくお願いします。
=end


Files


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #3715: Enumerator#size and #size=Rejected08/19/2010Actions
Actions #1

Updated by mrkn (Kenta Murata) about 14 years ago

=begin
むらたです。

permutation size の計算式を間違えてました。
修正したパッチを再度添付します。

On 2010/01/28, at 11:59, Kenta Murata wrote:

Feature #2673: the length for an enumerator generated by Array#permutation and Array#combination
http://redmine.ruby-lang.org/issues/show/2673

起票者: Kenta Murata
ステータス: Open, 優先度: Normal
カテゴリ: core, Target version: 1.9.2

Array#permutation と Array#combination が生成する enumerator は要素数が確定できますが、
Enumerator#length が存在しないため、to_a で配列化しなければ長さを取得できません。

この欠点を解消するため、Array#permutation と Array#combination が生成する enumerator に対して
length メソッドを特異メソッドとして追加するパッチを書きました。
差し支えないようでしたら trunk への取り込みを検討し頂けませんでしょうか。

よろしくお願いします。


http://redmine.ruby-lang.org

--
Kenta Murata
OpenPGP FP = FA26 35D7 4F98 3498 0810 E0D5 F213 966F E9EB 0BCC

本を書きました!!
『Ruby 逆引きレシピ』 http://www.amazon.co.jp/dp/4798119881/mrkn-22

E-mail:
twitter: http://twitter.com/mrkn/
blog: http://d.hatena.ne.jp/mrkn/

Attachment: 0001-array.c-rb_ary_permutation-rb_ary_combiation-test-ru.patch
=end

Actions #2

Updated by ujihisa (Tatsuhiro Ujihisa) about 14 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

=begin

=end

Actions #3

Updated by wanabe (_ wanabe) about 14 years ago

=begin
Enumerator#length は、一般に定義されていた方がなにかと便利な気がします。
また、同じクラスのオブジェクトが NoMethodError を起こしたり起こさなかったりするのは
少し分かりにくいので、なるべくなら避けた方がいいように思います。
長さが定義できないものについては、RuntimeError にするか nil を返すのはどうでしょうか。

とりあえず nil を返すようにして、ついでだったので例として Array#each についても
長さを返すようにしたパッチを書きました。
enumerator_length() を工夫すれば他にも応用できるようにしました(というつもりです)。
=end

Actions #4

Updated by mrkn (Kenta Murata) about 14 years ago

=begin
確かに、Enumerator#length が一般に定義されているほうが便利だと、私も思います。
wanabe さんの案に一票。

=end

Actions #5

Updated by matz (Yukihiro Matsumoto) about 14 years ago

=begin
まつもと ゆきひろです

In message "Re: [ruby-dev:40636] [Feature #2673] the length for an enumerator generated by Array#permutation and Array#combination"
on Sun, 14 Mar 2010 16:17:27 +0900, Kenta Murata writes:

|確かに、Enumerator#length が一般に定義されているほうが便利だと、私も思います。
|wanabe さんの案に一票。

なるほど。その場合、nilを返すのと、RuntimeErrorを発生させるの
でどちらが良いと思いますか? どちらにもそれなりに利点はあると
思いますが。前者は軽いとか、後者はエラーが明示されるとか。

fail earlyの原則からは例外の方が良いのかな。

=end

Actions #6

Updated by mame (Yusuke Endoh) about 14 years ago

=begin
遠藤です。

2010年3月14日13:05 _ wanabe :

Enumerator#length は、一般に定義されていた方がなにかと便利な気がします。
また、同じクラスのオブジェクトが NoMethodError を起こしたり起こさなかったりするのは
少し分かりにくいので、なるべくなら避けた方がいいように思います。
長さが定義できないものについては、RuntimeError にするか nil を返すのはどうでしょうか。

とりあえず nil を返すようにして、ついでだったので例として Array#each についても
長さを返すようにしたパッチを書きました。
enumerator_length() を工夫すれば他にも応用できるようにしました(というつもりです)。

length が Enumerator 生成時に確定できる場合はどのくらいあるんでしょう。
Array#each でも、元配列が書き換えられた場合を考えると困ります。

a = [1, 2, 3]
e = a.each
p e.length #=> 3
a << 4
p e.length #=> 4

wanabe さんのパッチでは length として配列をもたせられるようにして上記の
コードに対応しているようですが、permutation/combination には対応できない
ですよね。

a = [1, 2, 3]
e = a.permutation
p e.length #=> 6
a << 4
p e.length #=> 6
p e.to_a.length #=> 24

これに対応するためには、length として Proc か関数ポインタを保持せざるを
得なくなって不幸です。

やるとしたら、length は「Enumerator 生成時での長さを返す」と揃えるのが
いいのではないでしょうか。

--
Yusuke ENDOH

=end

Actions #7

Updated by mrkn (Kenta Murata) about 14 years ago

=begin
むらたです。

On 2010/03/22, at 12:42, Yusuke ENDOH wrote:

2010年3月14日13:05 _ wanabe :

Enumerator#length は、一般に定義されていた方がなにかと便利な気がします。
また、同じクラスのオブジェクトが NoMethodError を起こしたり起こさなかったりするのは
少し分かりにくいので、なるべくなら避けた方がいいように思います。
長さが定義できないものについては、RuntimeError にするか nil を返すのはどうでしょうか。

とりあえず nil を返すようにして、ついでだったので例として Array#each についても
長さを返すようにしたパッチを書きました。
enumerator_length() を工夫すれば他にも応用できるようにしました(というつもりです)。

length が Enumerator 生成時に確定できる場合はどのくらいあるんでしょう。
Array#each でも、元配列が書き換えられた場合を考えると困ります。

snip

やるとしたら、length は「Enumerator 生成時での長さを返す」と揃えるのが
いいのではないでしょうか。

良いと思います。

--
Kenta Murata
OpenPGP FP = FA26 35D7 4F98 3498 0810 E0D5 F213 966F E9EB 0BCC

本を書きました!!
『Ruby 逆引きレシピ』 http://www.amazon.co.jp/dp/4798119881/mrkn-22

E-mail:
twitter: http://twitter.com/mrkn/
blog: http://d.hatena.ne.jp/mrkn/

=end

Actions #8

Updated by shyouhei (Shyouhei Urabe) about 14 years ago

=begin
Kenta Murata さんは書きました:

やるとしたら、length は「Enumerator 生成時での長さを返す」と揃えるのが
いいのではないでしょうか。

良いと思います。

いや、どうなんでしょうねえ。おおもとの前提条件である

Array#permutation と Array#combination が生成する enumerator
は要素数が確定できますが、

に疑問が投げかけられているのだと思いましたが。そしてto_aすれば要素数の変化には
(Arrayのコンテクストで)対応できるわけだから、「lengthが知りたければArrayにせ
よ」というのはそれなりに妥当な指針なようにも思うのですけども。

Attachment: signature.asc
=end

Actions #9

Updated by mame (Yusuke Endoh) about 14 years ago

=begin
遠藤です。

2010年3月23日11:28 Urabe Shyouhei :

Kenta Murata さんは書きました:

やるとしたら、length は「Enumerator 生成時での長さを返す」と揃えるのが
いいのではないでしょうか。

良いと思います。

いやあ、気分は良くないですよね。自分で言っといて。

おおもとの前提条件である

Array#permutation と Array#combination が生成する enumerator
は要素数が確定できますが、

に疑問が投げかけられているのだと思いましたが。

そういう意図はなかったのですが、言われてみればそんな気がします。

そしてto_aすれば要素数の変化には
(Arrayのコンテクストで)対応できるわけだから、「lengthが知りたければArrayにせ
よ」というのはそれなりに妥当な指針なようにも思うのですけども。

この指針は実用上不便すぎるので、長さを前もって知る方法があれば
いいなあとは思います。しかしそのために手間がかかるのであれば、
需要とのトレードオフですかね。
「長さを前もって知りたい」という需要はどのくらいあるんでしょう。

Array が frozen の時だけ length がわかる、とかどうかなあ。
ほとんど隠し機能になっちゃうか。

--
Yusuke ENDOH

=end

Actions #10

Updated by wanabe (_ wanabe) about 14 years ago

=begin
ワナベです。

うっかりこんな時期になってしまいましたが(すみません)、この件どうしましょう。
未決定の事項が二つもあるので、どうしても 1.9.2 に導入したいという方からの
非常に強力な説得でもない限り、1.9.2 では見送りになってしまいそうですが
どなたかご意見ありませんでしょうか。

個人的には、どうしても 1.9.2 に、とまでは思いませんので

見送られるならそれでもよいのですが、困る人はいないのかが気になりました。

=end

Actions #11

Updated by mrkn (Kenta Murata) about 14 years ago

=begin
むらたです。

On 2010/03/31, at 2:15, _ wanabe wrote:

ワナベです。

うっかりこんな時期になってしまいましたが(すみません)、この件どうしましょう。
未決定の事項が二つもあるので、どうしても 1.9.2 に導入したいという方からの
非常に強力な説得でもない限り、1.9.2 では見送りになってしまいそうですが
どなたかご意見ありませんでしょうか。

個人的には、どうしても 1.9.2 に、とまでは思いませんので

見送られるならそれでもよいのですが、困る人はいないのかが気になりました。

私も当事者の一人ではありますが、1.9.3 or later で取り入れる事を目標に、
良い設計を追求したほうが良いと思っています。

--
Kenta Murata
OpenPGP FP = FA26 35D7 4F98 3498 0810 E0D5 F213 966F E9EB 0BCC

本を書きました!!
『Ruby 逆引きレシピ』 http://www.amazon.co.jp/dp/4798119881/mrkn-22

E-mail:
twitter: http://twitter.com/mrkn/
blog: http://d.hatena.ne.jp/mrkn/

=end

Actions #12

Updated by marcandre (Marc-Andre Lafortune) about 14 years ago

=begin
I just noticed this feature request.

Sadly, google translate is terrible for Japanese.

I wanted to propose that Enumerator had a #length/#size method that would return nil or the size of the enumeration, when known. My (unfinished) proposal was to allow a block though, so that the result would always be valid, even if the source object was modified. Object#to_enum would accept a block and use that to calculate the length.

I would really appreciate if someone was kind enough to summarize the state of this request for the use of those of us not blessed with Japanese reading skills!

=end

Actions #13

Updated by matz (Yukihiro Matsumoto) about 14 years ago

=begin
Hi,

In message "Re: [ruby-dev:40882] [Feature #2673] the length for an enumerator generated by Array#permutation and Array#combination"
on Thu, 1 Apr 2010 00:11:02 +0900, Marc-Andre Lafortune writes:

|I wanted to propose that Enumerator had a #length/#size method that would return nil or the size of the enumeration, when known. My (unfinished) proposal was to allow a block though, so that the result would always be valid, even if the source object was modified. Object#to_enum would accept a block and use that to calculate the length.

This propose has been suspended for 1.9.2 since the resule may not be
trustworthy considering the source modification after generating
enumerator. I am not sure passing block is a good idea or not, yet.
It's not intuitive for me at the first glance.

						matz.

=end

Actions #14

Updated by znz (Kazuhiro NISHIYAMA) about 14 years ago

  • Target version changed from 1.9.2 to 2.0.0

=begin

=end

Updated by ko1 (Koichi Sasada) over 11 years ago

  • Description updated (diff)
  • Target version changed from 2.0.0 to 2.6

I changed target to next minor because there is no discussion about it.

Updated by mame (Yusuke Endoh) over 6 years ago

  • Status changed from Assigned to Closed

This feature was accepted and introduced at #6636, wasn't it? Closing.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0