# [Feature #2673] the length for an enumerator generated by Array#permutation and Array#combination

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

e\$B5/I<<Te(B: Kenta M.
e\$B%9%F!<%?%9e(B: Open, e\$BM%@hEYe(B: Normal
e\$B%+%F%4%je(B: core, Target version: 1.9.2

Array#permutation e\$B\$He(B Array#combination e\$B\$,@8@.\$9\$ke(B enumerator
e\$B\$OMWAG?t\$,3NDj\$G\$-\$^\$9\$,!"e(B
Enumerator#length e\$B\$,B8:_\$7\$J\$\$\$?\$a!"e(Bto_a
e\$B\$GG[Ns2=\$7\$J\$1\$l\$PD9\$5\$r<hF@\$G\$-\$^\$;\$s!#e(B

e\$B\$3\$N7gE@\$r2r>C\$9\$k\$?\$a!"e(BArray#permutation e\$B\$He(B
Array#combination e\$B\$,@8@.\$9\$ke(B enumerator e\$B\$KBP\$7\$Fe(B
length e\$B%a%=%C%I\$rFC0[%a%=%C%I\$H\$7\$FDI2C\$9\$k%Q%C%A\$r=q\$-\$^\$7\$?!#e(B
e\$B:9\$7;Y\$(\$J\$\$\$h\$&\$G\$7\$?\$ie(B trunk
e\$B\$X\$N<h\$j9~\$_\$r8!F\$\$7D:\$1\$^\$;\$s\$G\$7\$g\$&\$+!#e(B

e\$B\$h\$m\$7\$/\$*4j\$\$\$7\$^\$9!#e(B

ã‚€ã‚‰ãŸã§ã™ã€‚

permutation size ã®è¨ˆç®—å¼ã‚’é–“é•ãˆã¦ã¾ã—ãŸã€‚
ä¿®æ­£ã—ãŸãƒ‘ãƒƒãƒã‚’å†åº¦æ·»ä»˜ã—ã¾ã™ã€‚

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

ã“ã®æ¬ ç‚¹ã‚’è§£æ¶ˆã™ã‚‹ãŸã‚ã€Array#permutation ã¨ Array#combination ãŒç”Ÿæˆã™ã‚‹ enumerator ã«å¯¾ã—ã¦
length ãƒ¡ã‚½ãƒƒãƒ‰ã‚’ç‰¹ç•°ãƒ¡ã‚½ãƒƒãƒ‰ã¨ã—ã¦è¿½åŠ ã™ã‚‹ãƒ‘ãƒƒãƒã‚’æ›¸ãã¾ã—ãŸã€‚
å·®ã—æ”¯ãˆãªã„ã‚ˆã†ã§ã—ãŸã‚‰ trunk ã¸ã®å–ã‚Šè¾¼ã¿ã‚’æ¤œè¨Žã—é ‚ã‘ã¾ã›ã‚“ã§ã—ã‚‡ã†ã‹ã€‚

ã‚ˆã‚ã—ããŠé¡˜ã„ã—ã¾ã™ã€‚

http://redmine.ruby-lang.org

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

æœ¬ã‚’æ›¸ãã¾ã—ãŸ!!
ã€ŽRuby é€†å¼•ããƒ¬ã‚·ãƒ”ã€ http://www.amazon.co.jp/dp/4798119881/mrkn-22

E-mail: [email protected]
blog: ドレッシングのような

e\$B%A%1%C%He(B #2673 e\$B\$,99?7\$5\$l\$^\$7\$?!#e(B (by _ wanabe)

e\$B%U%!%\$%ke(B enum_len.patch e\$BDI2Ce(B

Enumerator#length
e\$B\$O!"0lHL\$KDj5A\$5\$l\$F\$\$\$?J}\$,\$J\$K\$+\$HJXMx\$J5\$\$,\$7\$^\$9!#e(B
e\$B\$^\$?!“F1\$8%/%i%9\$N%*%V%8%'%/%H\$,e(B NoMethodError
e\$B\$r5/\$3\$7\$?\$j5/\$3\$5\$J\$+\$C\$?\$j\$9\$k\$N\$Oe(B
e\$B>/\$7J,\$+\$j\$K\$/\$\$\$N\$G!”\$J\$k\$Y\$/\$J\$iHr\$1\$?J}\$,\$\$\$\$\$h\$&\$K;W\$\$\$^\$9!#e(B
e\$BD9\$5\$,Dj5A\$G\$-\$J\$\$\$b\$N\$K\$D\$\$\$F\$O!"e(BRuntimeError e\$B\$K\$9\$k\$+e(B nil
e\$B\$rJV\$9\$N\$O\$I\$&\$G\$7\$g\$&\$+!#e(B

## e\$B\$H\$j\$“\$(\$:e(B nil e\$B\$rJV\$9\$h\$&\$K\$7\$F!”\$D\$\$\$G\$@\$C\$?\$N\$GNc\$H\$7\$Fe(B Array#each e\$B\$K\$D\$\$\$F\$be(B e\$BD9\$5\$rJV\$9\$h\$&\$K\$7\$?%Q%C%A\$r=q\$-\$^\$7\$?!#e(B enumerator_length() e\$B\$r9)IW\$9\$l\$PB>\$K\$b1~MQ\$G\$-\$k\$h\$&\$K\$7\$^\$7\$?!J\$H\$\$\$&\$D\$b\$j\$G\$9!K!#e(B

http://redmine.ruby-lang.org/issues/show/2673

e\$B\$^\$D\$b\$He(B e\$B\$f\$-\$R\$m\$G\$9e(B

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 M.
[email protected] writes:

|e\$B3N\$+\$K!“e(BEnumerator#length e\$B\$,0lHL\$KDj5A\$5\$l\$F\$\$\$k\$[\$&\$,JXMx\$@\$H!”;d\$b;W\$\$\$^\$9!#e(B
|wanabe e\$B\$5\$s\$N0F\$K0lI<!#e(B

e\$B\$J\$k\$[\$I!#\$=\$N>l9g!"e(Bnile\$B\$rJV\$9\$N\$H!“e(BRuntimeErrore\$B\$rH/@8\$5\$;\$k\$Ne(B
e\$B\$G\$I\$A\$i\$,NI\$\$\$H;W\$\$\$^\$9\$+!)e(B
e\$B\$I\$A\$i\$K\$b\$=\$l\$J\$j\$KMxE@\$O\$”\$k\$He(B
e\$B;W\$\$\$^\$9\$,!#A0<T\$O7Z\$\$\$H\$+!"8e<T\$O%(%i!<\$,L@<(\$5\$l\$k\$H\$+!#e(B

fail earlye\$B\$N86B’\$+\$i\$ONc30\$NJ}\$,NI\$\$\$N\$+\$J!#e(B

e\$B%A%1%C%He(B #2673 e\$B\$,99?7\$5\$l\$^\$7\$?!#e(B (by Kenta M.)

e\$B3N\$+\$K!“e(BEnumerator#length
e\$B\$,0lHL\$KDj5A\$5\$l\$F\$\$\$k\$[\$&\$,JXMx\$@\$H!”;d\$b;W\$\$\$^\$9!#e(B
wanabe e\$B\$5\$s\$N0F\$K0lI<!#e(B

http://redmine.ruby-lang.org/issues/show/2673

e\$B1sF#\$G\$9!#e(B

2010e\$BG/e(B3e\$B7ne(B14e\$BF|e(B13:05 _ wanabe [email protected]:

Enumerator#length e\$B\$O!"0lHL\$KDj5A\$5\$l\$F\$\$\$?J}\$,\$J\$K\$+\$HJXMx\$J5\$\$,\$7\$^\$9!#e(B
e\$B\$^\$?!“F1\$8%/%i%9\$N%*%V%8%'%/%H\$,e(B NoMethodError e\$B\$r5/\$3\$7\$?\$j5/\$3\$5\$J\$+\$C\$?\$j\$9\$k\$N\$Oe(B
e\$B>/\$7J,\$+\$j\$K\$/\$\$\$N\$G!”\$J\$k\$Y\$/\$J\$iHr\$1\$?J}\$,\$\$\$\$\$h\$&\$K;W\$\$\$^\$9!#e(B
e\$BD9\$5\$,Dj5A\$G\$-\$J\$\$\$b\$N\$K\$D\$\$\$F\$O!"e(BRuntimeError e\$B\$K\$9\$k\$+e(B nil e\$B\$rJV\$9\$N\$O\$I\$&\$G\$7\$g\$&\$+!#e(B

e\$B\$H\$j\$“\$(\$:e(B nil e\$B\$rJV\$9\$h\$&\$K\$7\$F!”\$D\$\$\$G\$@\$C\$?\$N\$GNc\$H\$7\$Fe(B Array#each e\$B\$K\$D\$\$\$F\$be(B
e\$BD9\$5\$rJV\$9\$h\$&\$K\$7\$?%Q%C%A\$r=q\$-\$^\$7\$?!#e(B
enumerator_length() e\$B\$r9)IW\$9\$l\$PB>\$K\$b1~MQ\$G\$-\$k\$h\$&\$K\$7\$^\$7\$?!J\$H\$\$\$&\$D\$b\$j\$G\$9!K!#e(B

length e\$B\$,e(B Enumerator
e\$B@8@.;~\$K3NDj\$G\$-\$k>l9g\$O\$I\$N\$/\$i\$\$\$"\$k\$s\$G\$7\$g\$&!#e(B
Array#each e\$B\$G\$b!"85G[Ns\$,=q\$-49\$(\$i\$l\$?>l9g\$r9M\$(\$k\$H:\$\$j\$^\$9!#e(B

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

wanabe e\$B\$5\$s\$N%Q%C%A\$G\$Oe(B length
e\$B\$H\$7\$FG[Ns\$r\$b\$?\$;\$i\$l\$k\$h\$&\$K\$7\$F>e5-\$Ne(B
e\$B%3!<%I\$KBP1~\$7\$F\$\$\$k\$h\$&\$G\$9\$,!"e(Bpermutation/combination
e\$B\$K\$OBP1~\$G\$-\$J\$\$e(B
e\$B\$G\$9\$h\$M!#e(B

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

e\$B\$3\$l\$KBP1~\$9\$k\$?\$a\$K\$O!"e(Blength e\$B\$H\$7\$Fe(B Proc
e\$B\$+4X?t%]%\$%s%?\$rJ];}\$;\$6\$k\$re(B
e\$BF@\$J\$/\$J\$C\$FIT9,\$G\$9!#e(B

e\$B\$d\$k\$H\$7\$?\$i!"e(Blength e\$B\$O!Ve(BEnumerator
e\$B@8@.;~\$G\$ND9\$5\$rJV\$9!W\$HB7\$(\$k\$N\$,e(B
e\$B\$\$\$\$\$N\$G\$O\$J\$\$\$G\$7\$g\$&\$+!#e(B

Kenta M. ã•ã‚“ã¯æ›¸ãã¾ã—ãŸ:

ã‚„ã‚‹ã¨ã—ãŸã‚‰ã€length ã¯ã€ŒEnumerator ç”Ÿæˆæ™‚ã§ã®é•·ã•ã‚’è¿”ã™ã€ã¨æƒãˆã‚‹ã®ãŒ
ã„ã„ã®ã§ã¯ãªã„ã§ã—ã‚‡ã†ã‹ã€‚

è‰¯ã„ã¨æ€ã„ã¾ã™ã€‚

Array#permutation ã¨ Array#combination ãŒç”Ÿæˆã™ã‚‹ enumerator
ã¯è¦ç´ æ•°ãŒç¢ºå®šã§ãã¾ã™ãŒã€

ã«ç–‘å•ãŒæŠ•ã’ã‹ã‘ã‚‰ã‚Œã¦ã„ã‚‹ã®ã ã¨æ€ã„ã¾ã—ãŸãŒã€‚ãã—ã¦to_aã™ã‚Œã°è¦ç´ æ•°ã®å¤‰åŒ–ã«ã¯
(Arrayã®ã‚³ãƒ³ãƒ†ã‚¯ã‚¹ãƒˆã§)å¯¾å¿œã§ãã‚‹ã‚ã‘ã ã‹ã‚‰ã€ã€ŒlengthãŒçŸ¥ã‚ŠãŸã‘ã‚Œã°Arrayã«ã›

e\$B\$`\$i\$?\$G\$9!#e(B

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

length e\$B\$,e(B Enumerator e\$B@8@.;~\$K3NDj\$G\$-\$k>l9g\$O\$I\$N\$/\$i\$\$\$"\$k\$s\$G\$7\$g\$&!#e(B
Array#each e\$B\$G\$b!"85G[Ns\$,=q\$-49\$(\$i\$l\$?>l9g\$r9M\$(\$k\$H:\$\$j\$^\$9!#e(B

snip

e\$B\$d\$k\$H\$7\$?\$i!"e(Blength e\$B\$O!Ve(BEnumerator e\$B@8@.;~\$G\$ND9\$5\$rJV\$9!W\$HB7\$(\$k\$N\$,e(B
e\$B\$\$\$\$\$N\$G\$O\$J\$\$\$G\$7\$g\$&\$+!#e(B

e\$BNI\$\$\$H;W\$\$\$^\$9!#e(B

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

e\$BK\$r=q\$-\$^\$7\$?e(B!!
e\$B!Xe(BRuby e\$B5U0z\$-%l%7%T!Ye(B

E-mail: [email protected]
blog: ドレッシングのような

e\$B1sF#\$G\$9!#e(B

2010e\$BG/e(B3e\$B7ne(B23e\$BF|e(B11:28 Urabe S.
[email protected]:

Kenta M. e\$B\$5\$s\$O=q\$-\$^\$7\$?e(B:

e\$B\$d\$k\$H\$7\$?\$i!"e(Blength e\$B\$O!Ve(BEnumerator e\$B@8@.;~\$G\$ND9\$5\$rJV\$9!W\$HB7\$(\$k\$N\$,e(B
e\$B\$\$\$\$\$N\$G\$O\$J\$\$\$G\$7\$g\$&\$+!#e(B

e\$BNI\$\$\$H;W\$\$\$^\$9!#e(B

e\$B\$\$\$d\$"!"5\$J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+\$J\$”!#e(B
e\$B\$[\$H\$s\$I1#\$75!G=\$K\$J\$C\$A\$c\$&\$+!#e(B

e\$B%A%1%C%He(B #2673 e\$B\$,99?7\$5\$l\$^\$7\$?!#e(B (by _ wanabe)

e\$B%o%J%Y\$G\$9!#e(B

e\$B\$&\$C\$+\$j\$3\$s\$J;~4|\$K\$J\$C\$F\$7\$^\$\$\$^\$7\$?\$,!J\$9\$_\$^\$;\$s!K!“\$3\$N7o\$I\$&\$7\$^\$7\$g\$&!#e(B
e\$BL\$7hDj\$N;v9`\$,Fs\$D\$b\$”\$k\$N\$G!"\$I\$&\$7\$F\$be(B 1.9.2
e\$B\$KF3F~\$7\$?\$\$\$H\$\$\$&J}\$+\$i\$Ne(B
e\$BHs>o\$K6/NO\$J@bF@\$G\$b\$J\$\$8B\$j!“e(B1.9.2
e\$B\$G\$O8+Aw\$j\$K\$J\$C\$F\$7\$^\$\$\$=\$&\$G\$9\$,e(B
e\$B\$I\$J\$?\$+\$40U8+\$”\$j\$^\$;\$s\$G\$7\$g\$&\$+!#e(B

# e\$B8D?ME*\$K\$O!“\$I\$&\$7\$F\$be(B 1.9.2 e\$B\$K!”\$H\$^\$G\$O;W\$\$\$^\$;\$s\$N\$Ge(B

## e\$B8+Aw\$i\$l\$k\$J\$i\$=\$l\$G\$b\$h\$\$\$N\$G\$9\$,!":\$\$k?M\$O\$\$\$J\$\$\$N\$+\$,5\$\$K\$J\$j\$^\$7\$?!#e(B

http://redmine.ruby-lang.org/issues/show/2673

e\$B%A%1%C%He(B #2673 e\$B\$,99?7\$5\$l\$^\$7\$?!#e(B (by Marc-Andre L.)

I just noticed this feature request.

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

http://redmine.ruby-lang.org/issues/show/2673

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 L.
[email protected] 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.
``````

Hi,

On Wed, Mar 31, 2010 at 11:22 AM, Yukihiro M.
[email protected] wrote:

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.

Just another word in favor of a block: for some enumerators,
calculating the overall size might non trivial (even though less
costly then doing the actual enumeration).

For this reason, I feel that there should not be a performance penalty
when creating an Enumerator because the length need to be
precalculated, even though very well might not be used. That’s in
addition to the fact that it might be wrong, if the source was
modified in the meantime.

This is why I feel that lazily calculating the length, whenever
needed, is the best way to go, both for reasons of efficiency and
correctness.

I regret not having posted my ideas before, but hopefully we’ll get
Enumerator#length in 1.9.3…

e\$B\$`\$i\$?\$G\$9!#e(B

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

e\$B%o%J%Y\$G\$9!#e(B

e\$B\$&\$C\$+\$j\$3\$s\$J;~4|\$K\$J\$C\$F\$7\$^\$\$\$^\$7\$?\$,!J\$9\$_\$^\$;\$s!K!“\$3\$N7o\$I\$&\$7\$^\$7\$g\$&!#e(B
e\$BL\$7hDj\$N;v9`\$,Fs\$D\$b\$”\$k\$N\$G!"\$I\$&\$7\$F\$be(B 1.9.2 e\$B\$KF3F~\$7\$?\$\$\$H\$\$\$&J}\$+\$i\$Ne(B
e\$BHs>o\$K6/NO\$J@bF@\$G\$b\$J\$\$8B\$j!“e(B1.9.2 e\$B\$G\$O8+Aw\$j\$K\$J\$C\$F\$7\$^\$\$\$=\$&\$G\$9\$,e(B
e\$B\$I\$J\$?\$+\$40U8+\$”\$j\$^\$;\$s\$G\$7\$g\$&\$+!#e(B

# e\$B8+Aw\$i\$l\$k\$J\$i\$=\$l\$G\$b\$h\$\$\$N\$G\$9\$,!":\$\$k?M\$O\$\$\$J\$\$\$N\$+\$,5\$\$K\$J\$j\$^\$7\$?!#e(B

e\$B;d\$bEv;v<T\$N0l?M\$G\$O\$"\$j\$^\$9\$,!"e(B1.9.3 or later
e\$B\$G<h\$jF~\$l\$k;v\$rL\I8\$K!"e(B
e\$BNI\$\$@_7W\$rDI5a\$7\$?\$[\$&\$,NI\$\$\$H;W\$C\$F\$\$\$^\$9!#e(B

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

e\$BK\$r=q\$-\$^\$7\$?e(B!!
e\$B!Xe(BRuby e\$B5U0z\$-%l%7%T!Ye(B

E-mail: [email protected]