Array の product の戻り値

e$B>.GH$G$9!#e(B

Ruby 1.9 e$B$Ne(B Array e$B%/%i%9$Ne(B product
e$B%a%=%C%I$O;H$($=$&$@$H;W$C$F$$$ke(B
e$B$N$G$9$,!$JV$5$l$k$N$,G[Ns$NG[Ns$K$J$k$o$1$G$9$M!#e(B

e$B3N$+$K!V=89g$ND>@Q!W$H9M$($k$H!$=89g$C$]$$%%V%8%%/%H!$$3$3$G$Oe(B
e$BG[Ns$,JV$5$l$k$N$,$$$$$N$+$b$H$$$&?t3XE
$J463P$,J,$+$i$J$/$b$J$$$Ne(B
e$B$G$9$,!$e(Bpermutation e$B$de(B combination e$B$,e(B
Enumerable:Enumerator e$B%/%i%9e(B
e$B$N%*%V%8%%/%H$rJV$9$N$r$_$k$H!$F1$8$h$&$KF0:n$7$?J}$,$`$7$m0l4S@-e(B
e$B$,$"$k$h$&$K;W$$$^$9!#e(B

e$B$D$^$j!$7k2L$r$R$H$D$:$D<h$j=P$9$N$K!$<!$N$h$&$K=q$-J}$rJQ$($J$1$le(B
e$B$P$J$i$J$$$N$OLLGr$/$J$$$H8D?ME*$K46$8$k$o$1$9!#e(B

ar.permutation(n) {|x| p x}
ar.combination(n) {|x| p x}

ar.product(ar2).each {|x| p x}

e$BG[Ns$NG[Ns$@$C$?$i$I$&$;e(B each
e$B$G=hM}$9$k$@$m$&$H9M$($k$H!$$O$8$a$+e(B
e$B$ie(B each e$B$G=hM}$5$l$?e(B Enumerator
e$B$N7A$K$J$C$F$$$?$i!$$=$N$[$&$,;H$$e(B
e$B$d$9$=$&$@$7!#$I$s$J$b$N$G$7$g$&$+!)e(B

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

In message Re: [ruby-dev:33712] Array e$B$Ne(B product
e$B$NLa$jCMe(B
on Tue, 12 Feb 2008 15:25:46 +0900, Hideo Konami
[email protected] writes:

|Ruby 1.9 e$B$Ne(B Array e$B%/%i%9$Ne(B product e$B%a%=%C%I$O;H$($=$&$@$H;W$C$F$$$ke(B
|e$B$N$G$9$,!$JV$5$l$k$N$,G[Ns$NG[Ns$K$J$k$o$1$G$9$M!#e(B

e$B$O$$!#e(B

|e$B3N$+$K!V=89g$ND>@Q!W$H9M$($k$H!$=89g$C$]$$%%V%8%'%/%H!$$3$3$G$Oe(B
|e$BG[Ns$,JV$5$l$k$N$,$$$$$N$+$b$H$$$&?t3XE
$J463P$,J,$+$i$J$/$b$J$$$Ne(B
|e$B$G$9$,!$e(Bpermutation e$B$de(B combination e$B$,e(B Enumerable:Enumerator e$B%/%i%9e(B
|e$B$N%*%V%8%'%/%H$rJV$9$N$r$_$k$H!$F1$8$h$&$KF0:n$7$?J}$,$`$7$m0l4S@-e(B
|e$B$,$"$k$h$&$K;W$$$^$9!#e(B

permutatione$B$de(Bcombinatione$B$N>l9g$K$Oe(B

ar.permutation(n) {|x| p x}
ar.combination(n) {|x| p x}

e$B$H$$$&;H$$J}$,!VI8=`!W$G!"%V%m%C%/$r;XDj$7$J$$>l9g$K$Oe(B(e$BB>$Ne(B
e$B%V%m%C%/$r;XDj$9$k%a%=%C%I$K$J$i$$e(B)Enumeratore$B$rJV$7$F$$$^$9!#e(B

e$B0lJ}!e(Bproducte$B$N>l9g$K$O$$/$^$G$bD>@Q$r7W;;$9$k$H$$$&$3$H$G!"e(B
e$B%V%m%C%/$r;XDj$9$kJ}K!$rDs6!$7$F$$$^$;$s!#;d<+?H$O<B@83h$GG[e(B
e$BNs$ND>@Q$r;H$&$3$H$,$J$$$N$G6qBNE*$K%$%a!<%8$G$-$J$$$N$G$9$,!"e(B
e$BG[Ns$,JV$C$F$-$Fe(B

ar.product(ar2).each {|x| p x}

e$B$H$9$k$h$j$be(B

ar.product(ar2) {|x| p x}

e$B$H$9$kJ}$,$&$l$7$$$b$N$J$N$G$7$g$&$+!#$^$?!"e(BEnumeratore$B$rJV$9e(B
e$B$H$$$&$3$H$O!La$jCM$KBP$7$F$OB>$NG[NsE*%a%=%C%I$O;H$($J$$$oe(B
e$B$1$G$9$,!$=$NJU$O$I$&$J$N$G$7$g$&!)e(B

                            e$B$^$D$b$He(B e$B$f$-$R$me(B /:|)

e$B@.@%$G$9!#e(B

e$BHFMQe(B Indexer
e$B$rMQ0U$G$-$k$^$G@xIz$7$F$$$kM=Dj$@$C$?$N$G$9$,!"3J9%$NNc$,e(B
e$B$G$F$-$?$N$G!#e(B

Yukihiro M. wrote:

e$B$^$?!"e(BEnumeratore$B$rJV$9e(B
e$B$H$$$&$3$H$O!La$jCM$KBP$7$F$OB>$NG[NsE*%a%=%C%I$O;H$($J$$$oe(B
e$B$1$G$9$,!$=$NJU$O$I$&$J$N$G$7$g$&!)e(B

e$BNc$($P$3$l$,!"e(B[ruby-dev:33448] e$B$de(B [ruby-dev:33472]
e$B$G=P$7$?!"e(BIndexable
(e$B$H$$$&$+e(B Indexer) e$B$,M_$7$/$J$k;vNc$G$9!#e(B

e$B$"$l$+$iB>$NJ}$K65$($FD:$$$?$N$G$9$,!"!Ve(BEnumeratore$B!W$H!VG[NsE*%a%=%C%I$re(B
e$BHw$($?$b$N!W$O!Ve(BC++ e$B$K$*$1$ke(B
Iteratore$B!W$K0lHL2=$G$-!"$=$NJ,N`$K$h$k$H!"e(B

  • Enumerator -> Input Iterator
  • e$BG[NsE*%a%=%C%Ie(B -> Random Access Iterator
    e$B$K$"$?$k$=$&$G$9!#e(B

e$B$=$7$F!"e(B

  • e$B8=:e(B Ruby e$B$K$O:G$b$f$k$$e(B input iterator
    e$B$OB8:
    $9$k$,!"e(B
    e$B!!$=$l$h$j87$7$$$b$N$OA4$/$J$$e(B
  • e$B$h$C$F!"$b$C$H87$7$$$b$N$,M_$7$/$F$b!"G[Ns$K5M$a9~$`$+!"e(B
    e$B!!e(BEnumerator e$B$G2fK}$9$k$+!"FH<+%/%i%9$r:n$k$3$H$K$J$k!#e(B
  • e$B$@$+$i!"!VG[NsE*%a%=%C%I!W$rHw$($?e(B Enumeratore$B!"e(B
    e$B!!$D$^$je(B Indexer e$B$,M_$7$$!#e(B
  • e$B$J$!"!VG[NsE%a%=%C%I!W$N%-%b$Oe(B [], []=, length e$B$G$"$j!e(B
    e$B!!$3$l$i$,<BAu$5$l$F$$$l$P!$"$H$O$=$l$rMQ$$$F<BAu$G$-$ke(B
    e$B$H$$$&$3$H$,8@$$$?$+$C$?$N$G$9!#e(B

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

In message Re: [ruby-dev:33752] Re: Array e$B$Ne(B product
e$B$NLa$jCMe(B
on Wed, 13 Feb 2008 15:21:51 +0900, NARUSE, Yui
[email protected] writes:

|e$B$$l$+$iB>$NJ}$K65$($FD:$$$?$N$G$9$,!!Ve(BEnumeratore$B!W$H!VG[NsE*%a%=%C%I$re(B
|e$BHw$($?$b$N!W$O!Ve(BC++ e$B$K$$1$ke(B Iteratore$B!W$K0lHL2=$G$-!"$=$NJ,N`$K$h$k$H!"e(B
|
Enumerator Input Iterator
|* e$BG[NsE*%a%=%C%Ie(B Random Access Iterator
|e$B$K$"$?$k$=$&$G$9!#e(B

e$B<+J,$GJY6/$9$k$Y$-$J$N$+$b$7$l$^$;$s$,!:G6a$Ne(BC++e$B;v>p$K$OABe(B
e$B$$$N$Ge(B(e$B;d$,e(BC++e$B%W%m%0%i%^$@$C$?$N$Oe(B10e$BG/0J>eA0e(B)e$B!!Ve(BC++
e$B$K$*$1e(B
e$B$ke(B Iteratore$B!W$H$$$&$N$Oe(BInput Iteratore$B$He(BRandom Access
Iteratore$B$NB>$K$b$$$m$$$m$"$k$s$G$9$+$M!#e(B

|e$B$=$7$F!"e(B
|* e$B8=:e(B Ruby e$B$K$O:G$b$f$k$$e(B input iterator e$B$OB8:$9$k$,!e(B
|e$B!!$=$l$h$j87$7$$$b$N$OA4$/$J$$e(B
|* e$B$h$C$F!$b$C$H87$7$$$b$N$,M_$7$/$F$b!"G[Ns$K5M$a9~$`$+!"e(B
|e$B!!e(BEnumerator e$B$G2fK}$9$k$+!"FH<+%/%i%9$r:n$k$3$H$K$J$k!#e(B

e$B$^$"!"8@$C$F$k$3$H$OJ,$+$j$^$9!#e(B

|* e$B$@$+$i!!VG[NsE*%a%=%C%I!W$rHw$($?e(B Enumeratore$B!e(B
|e$B!!$D$^$je(B Indexer e$B$,M_$7$$!#e(B
|* e$B$J$!"!VG[NsE%a%=%C%I!W$N%-%b$Oe(B [], []=, length e$B$G$$j!e(B
|e$B!!$3$l$i$,<BAu$5$l$F$$$l$P!$$H$O$=$l$rMQ$$$F<BAu$G$-$ke(B
|e$B$H$$$&$3$H$,8@$$$?$+$C$?$N$G$9!#e(B

e$B$&!<$s!e(Blengthe$B$,I,?$G$$k$J$i$PG[Ns$K$7$F$7$^$C$F$$$1$J$$M}e(B
e$BM3$,$"$^$j;W$$$D$+$J$$$N$G$9$,!#e(Blengthe$B$,$J$$$J$i$Pe(BLazye$B$Je(BList
e$B$,:n$l$k$H$$$&M}M3$,;W$$$D$/$N$G$9$,!#%a%b%j8zN(!)e(B

e$B$b$&$A$g$C$H$$$m$$$m5DO@$7$J$$$H!"$J$<e(BRandom Access Iterator
e$B$,I,MW$J$N$+8+$($F$-$^$;$s!#e(B

                            e$B$^$D$b$He(B e$B$f$-$R$me(B /:|)

On 2/13/08, Yukihiro M. [email protected] wrote:

ar.product(ar2) {|x| p x}

e$B$H$9$kJ}$,$&$l$7$$$b$N$J$N$G$7$g$&$+!#$^$?!"e(BEnumeratore$B$rJV$9e(B
e$B$H$$$&$3$H$O!La$jCM$KBP$7$F$OB>$NG[NsE*%a%=%C%I$O;H$($J$$$oe(B
e$B$1$G$9$,!$=$NJU$O$I$&$J$N$G$7$g$&!)e(B

e$B$I$C$A$+$H$$$&$H!"$^$D$b$H$5$s$X$N1g73$J$N$G$9$,!e(B
e$B$`$+$7!<+J,$Ge(B * e$B$He(B **
e$B$r>e=q$-$7$FD>@Q%*%V%8%'%/%H$rJV$9$b$N$re(B
e$BDj5A$7$?$H$-$O!"e(B

for i,j,k,l in [true,false]*[-1,+1]**3
check(i,j,k,l)
end

e$B$$?$$$J;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/p/product/

http://www.notwork.org/~gotoken/ruby/p/as-is/combinatoria.rb

e$B$4$H$1$se(B

In article [email protected],
Yukihiro M. [email protected] writes:

e$B$H$9$kJ}$,$&$l$7$$$b$N$J$N$G$7$g$&$+!#e(B

e$B;W$$=P$7$?$s$G$9$,!"$7$i$_$D$V$7$K%F%9%H$r9T$&$H$-$KD>@Q$r<Be(B
e$B:]$K;H$C$F$$$^$9!#$?$H$($Pe(B test_m17n_comb.rb e$B$de(B
test_sprintf_comb.rb e$B$G$O$=$&$$$&%a%=%C%I$r$=$l$>$l$KDj5A$7e(B
e$B$F$$$^$9!#e(B

e$B$3$N>l9g!D>@Q$r0l5$$K@8@.$9$k%a%b$j>CHq$OITMW$J$N$G!%$%F%l!<e(B
e$B%?$N$[$&$,5$J,$,$h$m$7$$$H$$$&$N$O$"$j$^$9$M!#e(B

e$B$?$@!"8=;~E@$GD4$Y$F$_$?$H$3$m!e(B41e$BMWAG$NG[Ns$re(B
3e$B$DAH$_9g$o$;e(B
e$B$ke(B 68921e$B8D$N$,:GBg$G!@8@.$7$F$b$=$s$J$K:$$k$H$$$&$o$1$G$O$Je(B
e$B$$46$8$G$9$,!#e(B

e$B@.@%$G$9!#e(B

Yukihiro M. wrote:

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B
e$B<+J,$GJY6/$9$k$Y$-$J$N$+$b$7$l$^$;$s$,!:G6a$Ne(BC++e$B;v>p$K$OABe(B
e$B$$$N$Ge(B(e$B;d$,e(BC++e$B%W%m%0%i%^$@$C$?$N$Oe(B10e$BG/0J>eA0e(B)e$B!!Ve(BC++ e$B$K$*$1e(B
e$B$ke(B Iteratore$B!W$H$$$&$N$Oe(BInput Iteratore$B$He(BRandom Access
Iteratore$B$NB>$K$b$$$m$$$m$"$k$s$G$9$+$M!#e(B

e$B$!;2>H$rD%$m$&$H;W$C$FK:$l$F$$$^$7$?!#e(B
http://www.kuzbass.ru/docs/isocpp/lib-iterators.html

e$B?^$K$9$k$H0J2<$NDL$j!"e(B

Random access Bidirectional Forward Input
Output
e$B$=$l$>$l0J2<$N$h$&$K$J$j$^$9!#e(B
Input: e$BF~NO$N$_0lJ}8~%7!<%1%s%7%c%ke(B
Output: e$B=PNO$N$_0lJ}8~%7!<%1%s%7%c%ke(B
Forward: e$BF~=PNO0lJ}8~%7!<%1%s%7%c%ke(B
Bidirectional: e$BF~=PNOAPJ}8~%7!<%1%s%7%c%ke(B
Random access: e$BF~=PNO%i%s%@%`%"%/%;%9e(B

e$B$A$J$_$K!"e(Bboost e$B$@$H$A$g$C$HJQ$o$C$F!"e(B
Incrementable Single Pass Forward Traversal
Bidirectional Traversal Random Access Traversal
http://www.boost.org/libs/iterator/doc/new-iter-concepts.html
e$B$H$J$C$F$$$^$9$,!"6hJL$NK<A$OF1$8$G$9$M!#e(B

|* e$B$@$+$i!!VG[NsE*%a%=%C%I!W$rHw$($?e(B Enumeratore$B!e(B
|e$B!!$D$^$je(B Indexer e$B$,M_$7$$!#e(B
|* e$B$J$!"!VG[NsE%a%=%C%I!W$N%-%b$Oe(B [], []=, length e$B$G$$j!e(B
|e$B!!$3$l$i$,<BAu$5$l$F$$$l$P!$$H$O$=$l$rMQ$$$F<BAu$G$-$ke(B
|e$B$H$$$&$3$H$,8@$$$?$+$C$?$N$G$9!#e(B

e$B$&!<$s!e(Blengthe$B$,I,?$G$$k$J$i$PG[Ns$K$7$F$7$^$C$F$$$1$J$$M}e(B
e$BM3$,$"$^$j;W$$$D$+$J$$$N$G$9$,!#%a%b%j8zN(!)e(B

e$B$^$:!"%a%b%j8zN($H<B9T8zN(N>J}$G$9!#$3$l$Oe(B String.bytes
e$B$,JV$9e(B Indexer
e$B$N>l9g$,$$$$Nc$+$H!#e(B

e$B<!$K!<BBN$r0l$D$K$7$?$$$H$$$&%1!<%9$G$9!#$3$l$be(B String.bytes
e$B$NNc$G$9e(B
e$B$,!$3$l$,@0?tNs$rJV$9>l9g!"$3$l$K$?$$$7$F=q$-9~$_$r9T$C$F$b85$Ne(B
String
e$B$,JQ99$5$l$^$;$s!#e(B

e$B2C$($F!"FH<+$N%a%=%C%I$rDI2C$7$?$$>l9g$G$9!#e(BArray
e$B$N%$%s%9%?%s%9$KFC0[%ae(B
e$B%=%C%I$r;H$&J}K!$OLLE]$G$9$7!e(BArray e$B$r7Q>5$7$?>l9g$Oe(B []
e$B$r:FDj5A$7$F$be(B
first e$B$,0MB8$7$F$$$J$$$N$G$3$l$b;H$($^$;$s!#FH<+%/%i%9$Ke(B Indexable
e$B$re(B
include e$B$9$kJ}K!$J$i$P!$3$l$b$9$C$-$j$7$^$9!#e(B

lengthe$B$,$J$$$J$i$Pe(BLazye$B$Je(BList e$B$,:n$l$k$H$$$&M}M3$,;W$$$D$/$N$G$9$,!#e(B

Requirements e$B$K$D$$$F$O5DO@$NM>CO$,$"$kE@$@$H;W$C$F$$$^$9!#:#2se(B
length
e$B$r2C$($?$N$Oe(B String.bytes
e$B$,JV$9$b$N$KBP$7$F$N:NMQ$r4|BT$7$F$$$k$+$i$G!"e(B
e$B$=$N$?$a$Ke(B length e$B$,I,MW$K$J$j$^$7$?!#e(B
length e$B$de(B []= e$B$N$J$$e(B Indexer
e$B$H$$$&$N$bB8:$7$&$k$H;W$C$F$$$^$9$,!"$^$:e(B
e$B$OL\2<M
$7$$e(B String.bytes e$B$KI,MW$J%a%=%C%I$H$7$F!"e(B[], []=,
length e$B$r5s$2e(B
e$B$^$7$?!#e(B

e$B$b$&$A$g$C$H$$$m$$$m5DO@$7$J$$$H!"$J$<e(BRandom Access Iterator
e$B$,I,MW$J$N$+8+$($F$-$^$;$s!#e(B

e$BL\2<$N<gD%$H$7$F$O!"e(B[ruby-dev:33447] e$B$Ne(B

e$B$&!<$s!$I$&$7$h$&$+$J$!#@lMQ%/%i%92=$7$?J}$,@-G=$,>e$,$k$He(B
e$B$+$$$$$3$H$b$$k$s$G$9$,$M$(!#$G$b!e(Bbytee$B%%/%;%9$He(Bbytee$BNs5s;Re(B
e$B$rF10l;k$9$k$3$H$K$J$s$H$J$/Dq9346$,$$j$^$9!#e(B
e$B$K$D$$$F$G!!Ve(Bbytee$B%%/%;%9$Oe(Bbytee$BNs5s;R$N1dD9$K$$k!W$H$$$&$b$N$G$9!#e(B
String.bytes.each{|x| p x}
String.bytes[idx]
String.bytes[idx]=
e$B$O$=$l$>$l<+A3$J$b$N$G$$k$H$$$&$N$,Bh0l$N<gD%$G$9!#e(B

e$B$h$C$Fe(B String.bytes e$B$Oe(B Random Access Iterator
e$B$rJV$9$Y$-$H$$$&$N$,BhFs!#e(B
e$B$=$l$re(B Enumerator e$B$K$D$C$3$`$N$O$5$9$,$K0c$$$9$.$k$@$m$&$+$ie(B
Indexer e$B$He(B
e$B$$$&JL$N$b$N$K$7$h$&!#e(B
e$BB>$N%*%V%8%'%/%H$K$b;H$($k$@$m$&$+$ie(B Indexable
e$B$b$"$k$H$$$$$M!#e(B
e$B$H$3$m$G!e(BString.bytes e$BA0Ds$@$He(B length e$B$,I,MW$@$M!#e(B
e$B$H$J$C$F!:#$K;j$k$H$$$&46$8$G$7$g$&$+!#e(B