Feature #2673
the length for an enumerator generated by Array#permutation and Array#combination
| Status: | Assigned | Start date: | 01/28/2010 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | % Done: | 0% |
||
| Category: | core | |||
| Target version: | 2.0.0 |
Description
Array#permutation と Array#combination が生成する enumerator は要素数が確定できますが、 Enumerator#length が存在しないため、to_a で配列化しなければ長さを取得できません。 この欠点を解消するため、Array#permutation と Array#combination が生成する enumerator に対して length メソッドを特異メソッドとして追加するパッチを書きました。 差し支えないようでしたら trunk への取り込みを検討し頂けませんでしょうか。 よろしくお願いします。
History
Updated by Kenta Murata about 2 years ago
むらたです。 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: mrkn@mrkn.jp 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
Updated by ujihisa . about 2 years ago
- Status changed from Open to Assigned
- Assignee set to Yukihiro Matsumoto
Updated by _ wanabe almost 2 years ago
- File enum_len.patch added
Enumerator#length は、一般に定義されていた方がなにかと便利な気がします。 また、同じクラスのオブジェクトが NoMethodError を起こしたり起こさなかったりするのは 少し分かりにくいので、なるべくなら避けた方がいいように思います。 長さが定義できないものについては、RuntimeError にするか nil を返すのはどうでしょうか。 とりあえず nil を返すようにして、ついでだったので例として Array#each についても 長さを返すようにしたパッチを書きました。 enumerator_length() を工夫すれば他にも応用できるようにしました(というつもりです)。
Updated by Kenta Murata almost 2 years ago
確かに、Enumerator#length が一般に定義されているほうが便利だと、私も思います。 wanabe さんの案に一票。
Updated by Yukihiro Matsumoto almost 2 years ago
まつもと ゆきひろです 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 <redmine@ruby-lang.org> writes: |確かに、Enumerator#length が一般に定義されているほうが便利だと、私も思います。 |wanabe さんの案に一票。 なるほど。その場合、nilを返すのと、RuntimeErrorを発生させるの でどちらが良いと思いますか? どちらにもそれなりに利点はあると 思いますが。前者は軽いとか、後者はエラーが明示されるとか。 fail earlyの原則からは例外の方が良いのかな。
Updated by Yusuke Endoh almost 2 years ago
遠藤です。 2010年3月14日13:05 _ wanabe <redmine@ruby-lang.org>: > 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 <mame@tsg.ne.jp>
Updated by Kenta Murata almost 2 years ago
むらたです。 On 2010/03/22, at 12:42, Yusuke ENDOH wrote: > 2010年3月14日13:05 _ wanabe <redmine@ruby-lang.org>: >> 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: mrkn@mrkn.jp twitter: http://twitter.com/mrkn/ blog: http://d.hatena.ne.jp/mrkn/
Updated by Shyouhei Urabe almost 2 years ago
Kenta Murata さんは書きました: >> やるとしたら、length は「Enumerator 生成時での長さを返す」と揃えるのが >> いいのではないでしょうか。 > > 良いと思います。 いや、どうなんでしょうねえ。おおもとの前提条件である > Array#permutation と Array#combination が生成する enumerator > は要素数が確定できますが、 に疑問が投げかけられているのだと思いましたが。そしてto_aすれば要素数の変化には (Arrayのコンテクストで)対応できるわけだから、「lengthが知りたければArrayにせ よ」というのはそれなりに妥当な指針なようにも思うのですけども。 Attachment: signature.asc
Updated by Yusuke Endoh almost 2 years ago
遠藤です。 2010年3月23日11:28 Urabe Shyouhei <shyouhei@ruby-lang.org>: > Kenta Murata さんは書きました: > >>> やるとしたら、length は「Enumerator 生成時での長さを返す」と揃えるのが >>> いいのではないでしょうか。 >> >> 良いと思います。 いやあ、気分は良くないですよね。自分で言っといて。 > おおもとの前提条件である > >> Array#permutation と Array#combination が生成する enumerator >> は要素数が確定できますが、 > > に疑問が投げかけられているのだと思いましたが。 そういう意図はなかったのですが、言われてみればそんな気がします。 > そしてto_aすれば要素数の変化には > (Arrayのコンテクストで)対応できるわけだから、「lengthが知りたければArrayにせ > よ」というのはそれなりに妥当な指針なようにも思うのですけども。 この指針は実用上不便すぎるので、長さを前もって知る方法があれば いいなあとは思います。しかしそのために手間がかかるのであれば、 需要とのトレードオフですかね。 「長さを前もって知りたい」という需要はどのくらいあるんでしょう。 Array が frozen の時だけ length がわかる、とかどうかなあ。 ほとんど隠し機能になっちゃうか。 -- Yusuke ENDOH <mame@tsg.ne.jp>
Updated by _ wanabe almost 2 years ago
ワナベです。 うっかりこんな時期になってしまいましたが(すみません)、この件どうしましょう。 未決定の事項が二つもあるので、どうしても 1.9.2 に導入したいという方からの 非常に強力な説得でもない限り、1.9.2 では見送りになってしまいそうですが どなたかご意見ありませんでしょうか。 # 個人的には、どうしても 1.9.2 に、とまでは思いませんので # 見送られるならそれでもよいのですが、困る人はいないのかが気になりました。
Updated by Kenta Murata almost 2 years ago
むらたです。 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: mrkn@mrkn.jp twitter: http://twitter.com/mrkn/ blog: http://d.hatena.ne.jp/mrkn/
Updated by Marc-Andre Lafortune almost 2 years ago
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!
Updated by Yukihiro Matsumoto almost 2 years ago
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 <redmine@ruby-lang.org> 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.
Updated by Kazuhiro NISHIYAMA almost 2 years ago
- Target version changed from 1.9.2 to 2.0.0