Enumerable#interleave

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

Enumerable#interleave e$B$H$$$&%a%=%C%I$rDI2C$7$F$O$I$&$+$H;W$$e(B
e$B$^$9!#e(BClojuree$B$J$I$G8+$i$l$k%a%=%C%I$G$9!#;EMM$Oe(B

/*

  • call-seq:
  • enum.interleave(arg, ...)                 => enumerator
    
  • enum.interleave(arg, ...) {|x| block }    => nil
    
  • Takes one element from enum and following arguments then
  • merges corresponding elements from each args into a flat
  • sequence. If a block is given, it is invoked for each element,
  • otherwise an array of elements is returned.
  • a = [ 4, 5, 6 ]
    
  • b = [ 7, 8, 9 ]
    
  • [1,2,3].interleave(a, b)      #=> [1, 4, 7, 2, 5, 8, 3, 6, 9]
    
  • [1,2].interleave(a,b)         #=> [1, 4, 7, 2, 5, 8, 6, 9]
    
  • a.interleave([1,2],[8])       #=> [4, 1, 8, 5, 2, 6]
    

*/

e$B$H$$$&46$8$G!#$3$l$r<BAu$7$F$?$ie(B Enumerable#zip [email protected]@.$9$kD9e(B
e$B$5$r%l%7!<%P!<$8$c$J$/$F!":GC;$Ne(B Enumerable e$B$K9g$o$;$k$Y$-$Ge(B
e$B$O$J$$$+$H46$8$k$h$&$K$J$C$?$1$I!"$=$l$O$^$?JL$NOC!#e(B

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

2010e$BG/e(B3e$B7ne(B13e$BF|e(B22:44 Yukihiro M.
[email protected]:

Enumerable#interleave e$B$H$$$&%a%=%C%I$rDI2C$7$F$O$I$&$+$H;W$$e(B
e$B$^$9!#e(BClojuree$B$J$I$G8+$i$l$k%a%=%C%I$G$9!#;EMM$Oe(B

e$BDs0F$K$O!"$J$K$O$H$b$"$l$^$:6qBNE*$JMQK!$,$H$F$b$H$F$b=EMW$G$9!#e(B
e$B6qBNE*$G%-%c%C%A!<$G$*$)$=$l$OM_$7$$$H;W$&$h$&$JNc$,$"$k$He(B
e$B6&46$r8F$S$d$9$$$N$G$O$J$$$G$7$g$&$+!#e(B

rdoc e$B$K$"$kNc$O$A$g$C$HL5L#4%Ag$G6&46$G$-$^$;$s!#e(B

  • a = [ 4, 5, 6 ]
    
  • b = [ 7, 8, 9 ]
    
  • [1,2,3].interleave(a, b)      #=> [1, 4, 7, 2, 5, 8, 3, 6, 9]
    
  • [1,2].interleave(a,b)         #=> [1, 4, 7, 2, 5, 8, 6, 9]
    
  • a.interleave([1,2],[8])       #=> [4, 1, 8, 5, 2, 6]
    

*/

e$B0z?t$H$7$F<u$1IU$1$k$b$N$,$O$C$-$j$7$^$;$s!#e(B
e$BG[[email protected]$1$J$N$+!"e(BEnumerator e$B0lHL$r<u$1IU$1$k$N$+!#e(B

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

In message “Re: [ruby-dev:40624] Re: Enumerable#interleave”
on Sat, 13 Mar 2010 23:40:28 +0900, Tanaka A. [email protected]
writes:

|e$BDs0F$K$O!"$J$K$O$H$b$"$l$^$:6qBNE*$JMQK!$,$H$F$b$H$F$b=EMW$G$9!#e(B
|e$B6qBNE*$G%-%c%C%A!<$G$*$)$=$l$OM_$7$$$H;W$&$h$&$JNc$,$"$k$He(B
|e$B6&46$r8F$S$d$9$$$N$G$O$J$$$G$7$g$&$+!#e(B
|
|rdoc e$B$K$"$kNc$O$A$g$C$HL5L#4%Ag$G6&46$G$-$^$;$s!#e(B

e$B$=$l$O;d$K$O<e$$NN0h$J$N$G!"@5D>$J$H$3$m!"$=$l$rI,?$K$5$l$ke(B
e$B$H%a%=%C%I$NDI2C$OFq$7$$$G$9$M!#[email protected]!“e(BHaskele$B$de(BClojuree$B$K$O$“e(B
e$B$k$N$G!”$=$l$J$j$K;H$($kJ,Ln$O$”$k$N$G$O$J$$$+$HA[A|$7$^$9!#e(B

|e$B0z?t$H$7$F<u$1IU$1$k$b$N$,$O$C$-$j$7$^$;$s!#e(B
|e$BG[[email protected]$1$J$N$+!"e(BEnumerator e$B0lHL$r<u$1IU$1$k$N$+!#e(B

zipe$BF1MM!"e(BEnumerable(to_enume$B$Ge(BEnumeratore$B$rJV$9$b$NA4HLe(B)e$B$r<u$1e(B
e$BIU$1$^$9!#e(B

[email protected]@%$G$9!#e(B

(2010/03/14 0:04), Yukihiro M. wrote:

e$B$=$l$O;d$K$O<e$$NN0h$J$N$G!"@5D>$J$H$3$m!"$=$l$rI,?$K$5$l$ke(B
e$B$H%a%=%C%I$NDI2C$OFq$7$$$G$9$M!#[email protected]!“e(BHaskele$B$de(BClojuree$B$K$O$“e(B
e$B$k$N$G!”$=$l$J$j$K;H$($kJ,Ln$O$”$k$N$G$O$J$$$+$HA[A|$7$^$9!#e(B

e$B!Ve(BPHPe$B$K$"[email protected]$He(B100%e$BDL$i$J$$$o$1$G!#e(B

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

In message “Re: [ruby-dev:40626] Re: Enumerable#interleave”
on Sun, 14 Mar 2010 00:44:24 +0900, “NARUSE, Yui”
[email protected] writes:

|e$B!Ve(BPHPe$B$K$"[email protected]$He(B100%e$BDL$i$J$$$o$1$G!#e(B

e$B$=$&$+$J!#!Ve(BPHPe$B$Ne(BAPIe$B$N$^$^[email protected]$H$=$&[email protected]$H$O;W$$$^$9$,!"$=$N5!e(B
e$BG=$rM_$7$$?M$,$$$k$H$$$&>Z5r$H$7$F$O%"%j$J$s$8$c$J$+$J!#e(B

2010e$BG/e(B3e$B7ne(B14e$BF|e(B0:04 Yukihiro M.
[email protected]:

e$B$=$l$O;d$K$O<e$$NN0h$J$N$G!"@5D>$J$H$3$m!"$=$l$rI,?$K$5$l$ke(B
e$B$H%a%=%C%I$NDI2C$OFq$7$$$G$9$M!#[email protected]!“e(BHaskele$B$de(BClojuree$B$K$O$“e(B
e$B$k$N$G!”$=$l$J$j$K;H$($kJ,Ln$O$”$k$N$G$O$J$$$+$HA[A|$7$^$9!#e(B

e$B:G6a!“e(BPractical API Design
e$B$H$$$&K$rFI$s$G$$$?$j$9$k$N$G$9$,!“e(B
API Reviews
e$B$H$$$&@a$,$”$C$F!”$$$/$D$+$N9L\$,$"$2$i$l$F$$$k$s$G$9$,!"e(B e$B$d$C$Q$j$H$$$&$+$J$s$H$$$&$+:G=i$N9L$,%f!<%9%1!<%9$K4p$E$$$?e(B API
e$B$+e(B
e$B$I$&$+$H$$$&OC$G!"$^$!!"$3$l$Oe(B Ruby e$B$K8B$i$:=EMW$J$o$1$G$9!#e(B
(e$B$3$NK$NCx<T$Oe(B NetBeans e$B$N?M$G$9!#e(B)

Clojure e$B$K$$$m$$$m$H$"$k5!G=$N$&$A!“e(Binterleave
e$B$KCmL$7$?M}M3$,e(B
e$B$-$C$H$”$k$O$:$G!"$=$l$r=q$1$P$$$$$H;W$&$s$G$9$,Fq$7$$$G$9$+$M!#e(B

e$B$b$A$m$sDs0F<T0J30$N$R$H$,%f!<%9%1!<%9$rDs6!$7$F$b$$$$$s$G$9$,!"e(B
e$B;d$K$O$A$g$C$HA[A|NO$,5Z$S$^$;$s$G$7$?!#e(B

e$BA[A|NO$NBe$o$j$K8!:wNO$r;H$&$H!"e(B
Clojure e$B$Ne(B interleave
[email protected]@$K$O%f!<%9%1!<%9$O=q$$$F$J$$$h$&$G$9!#e(B
http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/interleave

Haskell e$B$K$"$k$H$$$&Dj5A$O8+$D$+$j$^$;$s$G$7$?!#e(B
e$B$I$3$K$"$k$s$G$7$g$&e(B?

e$B$5$i$K8!:w$7$F$$$/$H!"e(Bjoin e$B$r0lHL2=$7$?$h$&$JOC$G$9$+$M$’!#e(B
http://en.wikibooks.org/wiki/Clojure_Programming/Getting_Started
e$B$Ne(B (interleave completions (repeat “\n”)) e$B$H$+!#e(B

e$B$=$l$J$ie(B join e$B$r;H$($h!"$H$$$&5$$b$7$^$9$,!"e(Bjoin
e$B$HHf3S$9$k$He(B

[“A”, “B”, “C”] e$B$+$ie(B “A\nB\nC\n” e$B$de(B “A,B,C,”
[email protected]@.$7$?$$$3$H$Oe(B
e$B2?2s$b$"$C$?3P$($,$"$k$N$G!“e(B
e$B$=$l$,e(B
ary.interleave([”\n"].cycle).join
e$B$"$k$$$Oe(B
ary.interleave(["\n"]*ary.length).join
e$B$H=q$1$k$h$&$K$J$k$N$O$&$l$7$$!"$+$J$!e(B?
e$BJ8;zNs$K$9$k$K$O7k6Ie(B join e$B$,I,[email protected]$7!"e(B
e$B$A$g$C$HD9$9$.$F$3$NMQES$r9M$([email protected]$N$b$N$rJL$K9M$($?$/$J$j$^$9$,!#e(B

e$B$^$?!“8!:w$7$F$$$k$H!”>e=R$NNc$b$=$&$J$s$G$9$,!“e(B
e$B0z?t$Ke(B (repeat “\n”)
e$B$N$h$&$J$N$,M?$($F$$$kNc$,$1$C$3$&$”$C$F!“e(B
[email protected]$j$rJ#?t<o$K$G$-$k$H$$$&[email protected]$,<B:]$KLr$KN)$D$N$Oe(B
e$B$8$D$O5)$J$N$+$b!”$H$$$&5?$$$b46$8$^$9!#e(B

zipe$BF1MM!"e(BEnumerable(to_enume$B$Ge(BEnumeratore$B$rJV$9$b$NA4HLe(B)e$B$r<u$1e(B
e$BIU$1$^$9!#e(B

e$B$5$i$K;W$C$?$s$G$9$,!“e(B
interleave e$B$N0z?t$Ke(B cycle
e$BEy$NL58B%j%9%H!”$"$k$$$O%l%7!<%P$h$j$bD9$$$b$N$re(B
e$BM?$($?$H$-$K$O$I$&$J$k$s$G$7$g$&$+!#e(B

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

In message “Re: [ruby-dev:40628] Re: Enumerable#interleave”
on Sun, 14 Mar 2010 01:11:20 +0900, Tanaka A. [email protected]
writes:

|Clojure e$B$K$$$m$$$m$H$"$k5!G=$N$&$A!“e(Binterleave e$B$KCmL$7$?M}M3$,e(B
|e$B$-$C$H$”$k$O$:$G!"$=$l$r=q$1$P$$$$$H;W$&$s$G$9$,Fq$7$$$G$9$+$M!#e(B
|
|e$B$b$A$m$sDs0F<T0J30$N$R$H$,%f!<%9%1!<%9$rDs6!$7$F$b$$$$$s$G$9$,!"e(B
|e$B;d$K$O$A$g$C$HA[A|NO$,5Z$S$^$;$s$G$7$?!#e(B
|
|e$BA[A|NO$NBe$o$j$K8!:wNO$r;H$&$H!"e(B
|Clojure e$B$Ne(B interleave [email protected]@$K$O%f!<%9%1!<%9$O=q$$$F$J$$$h$&$G$9!#e(B
|http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/interleave

e$B$"$j$^$;$s$M!#e(B

|Haskell e$B$K$"$k$H$$$&Dj5A$O8+$D$+$j$^$;$s$G$7$?!#e(B
|e$B$I$3$K$"$k$s$G$7$g$&e(B?

e$B$A$g$C$HD4$Y$F$_$?$N$G$9$,!“e(BHaskelle$B$KAH$9~$$G$”$k$N$G$O$Je(B
e$B$$$h$&$G$9!#$,!"e(Bpermutatione$B$NDj5A$Ke(B(e$B<+A0$GDj5A$7$Fe(B)e$B;H$o$l$Fe(B
e$B$$$k$h$&$G$9!#e(B

– permutations xs returns the list of all permutations of xs.
– e.g., permutations “abc” == [“abc”,“bac”,“bca”,“acb”,“cab”,“cba”]
permutations :: [a] -> [[a]]
permutations [] = [[]]
permutations (x:xs) = [zs | ys <- permutations xs, zs <- interleave
x ys ]
where interleave :: a -> [a] -> [[a]]
interleave x [] = [[x]]
interleave x (y:ys) = [x:y:ys] ++ map (y:) (interleave x ys)

e$B$O$+$i$:$be(Binterleavee$B$N%f!<%9%1!<%9$K$J$C$F$k$N$+$b$7$l$^$;$s!#e(B

|e$B$5$i$K8!:w$7$F$$$/$H!“e(Bjoin e$B$r0lHL2=$7$?$h$&$JOC$G$9$+$M$’!#e(B
|http://en.wikibooks.org/wiki/Clojure_Programming/Getting_Started
|e$B$Ne(B (interleave completions (repeat “\n”)) e$B$H$+!#e(B
|
|e$B$=$l$J$ie(B join e$B$r;H$($h!”$H$$$&5$$b$7$^$9$,!“e(Bjoin e$B$HHf3S$9$k$He(B
|* e$B:G8e$NMWAG$N=$o$j$K$b$D$1$i$l$ke(B
|
[email protected]$j$rJ#?t<o$K$G$-$ke(B
|* e$BJ8;zNs0J30$K$b;H$($ke(B
|e$B$H$$$&$”$?$j$,[email protected]$K$J$k$G$7$g$&$+!#e(B

e$B$"$H!“e(Bjoine$B$O$/$C$D$1$FJ8;zNs$K$7$F$7$^$&$N$G!”$=$l$,$&$l$7$$e(B
e$B;~$H!"$&$l$7$/$J$$;~$,$"$k$G$7$g$&$M!#e(B

|e$B$5$i$K;W$C$?$s$G$9$,!“e(B
|interleave e$B$N0z?t$Ke(B cycle e$BEy$NL58B%j%9%H!”$"$k$$$O%l%7!<%P$h$j$bD9$$$b$N$re(B
|e$BM?$($?$H$-$K$O$I$&$J$k$s$G$7$g$&$+!#e(B

e$B:#2s$N<BAu$G$O!"0lHVC;$$e(BEnumerablee$B$+$i$NMWAG$r;H$$$-$C$?;[email protected](B
e$B$GDd;_$7$^$9!#e(BClojuree$B$b$=$&$$$&5sF0$G$7$?!#e(B

http://code.activestate.com/recipes/511480-interleaving-sequences/

e$B$G>R2p$5$l$F$$$ke(BPythone$BHG$O!"$J$<$+;D$C$?$b$N$r<h$j=P$7B3$1$ke(B
e$B5sF0$K$J$C$F$$$^$9$,!"$3$l$O;H$$$K$/$$$s$8$c$J$$$+$J$"!#e(B

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

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

In message “Re: [ruby-dev:40633] Re: Enumerable#interleave”
on Sun, 14 Mar 2010 11:28:10 +0900, Tanaka A. [email protected]
writes:

|> e$B$O$+$i$:$be(Binterleavee$B$N%f!<%9%1!<%9$K$J$C$F$k$N$+$b$7$l$^$;$s!#e(B
|
|e$B$3$Ne(B interleave e$B$O$=$Ne(B interleave e$B$G$O$J$$$h$&$J5$$,$7$^$9!#e(B

e$B$=$&$+!#e(BHaskelle$B$,$<$s$<$sFI$a$J$$$N$,O*8+$7$^$9$M!#CQ$:$+$7e(B
e$B$$$J!#e(B

|zip e$B$G0JA0!"$=$N$"$?$j$,JQ$o$C$FLa$C$?$h$&$J5-21$,$"$k$s$G$9$,!"e(B
|e$B$I$&$G$7$?$C$1e(B?

e$B3N$+$Ke([email protected]$3$H$,$"$C$F!“e([email protected]$7$?e(B
e$B$3$H$,$”$j$^$9!#7k6I!"$=$N;~$O%l%7!<%P!<$ND9$5M%@h$H$$$&[email protected](B
e$B$r=P$7$?$N$G$9$,!":#;W$($P$"$^$jNI$/$J$+$C$?$J$"$H!#$?$H$($P!"e(B
with_indexe$B$N$h$&$J$b$N$re(Bzipe$B$r;H$C$F:n$k>l9g!"O"HV$,@hF,$KMhe(B
e$B$l$J$$$H$$$&[email protected])Ls$N$h$&$J5$$,$7$^$9!#e(B

|> http://code.activestate.com/recipes/511480-interleaving-sequences/
|>
|> e$B$G>R2p$5$l$F$$$ke(BPythone$BHG$O!"$J$<$+;D$C$?$b$N$r<h$j=P$7B3$1$ke(B
|> e$B5sF0$K$J$C$F$$$^$9$,!"$3$l$O;H$$$K$/$$$s$8$c$J$$$+$J$"!#e(B
|
|e$B$=$3$GF,$NCf$KIb$+$s$G$$$kMQES$r=q$/$N$G$9!#e(B

e$B9M$($?$N$O!"e(Binterlavee$BBP>]$KL58BNs$r4^$`>l9g$G$9!#$?$H$($Pe(B
with_indexe$B$re(Bflattene$B$7$?$h$&[email protected]$k;~$K!"Dd;_$7$J$$$N$Oe(B
e$B;H$$$K$/$+$m$&$H!#e(B

|e$B$=$l$H$bK\Ev$KMQES$r;W$$Ib$+$Y$:$K;H$$$K$/$$$H46$8$k$s$G$7$g$&$+!#e(B
|e$B$=$&[email protected]$H$7$?$i=q$1$J$$$+$b$7$l$^$;$s$,!"$=$&$J$N$+$J$!!#e(B

e$B$I$&$b!"6qBNE*$K$J$j$-$l$J$$$N$,$$$1$^$;$s$M!#e(B

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

2010e$BG/e(B3e$B7ne(B14e$BF|e(B16:36 Yukihiro M.
[email protected]:

e$B3N$+$Ke([email protected]$3$H$,$"$C$F!“e([email protected]$7$?e(B
e$B$3$H$,$”$j$^$9!#7k6I!"$=$N;~$O%l%7!<%P!<$ND9$5M%@h$H$$$&[email protected](B
e$B$r=P$7$?$N$G$9$,!":#;W$($P$"$^$jNI$/$J$+$C$?$J$"$H!#$?$H$($P!"e(B
with_indexe$B$N$h$&$J$b$N$re(Bzipe$B$r;H$C$F:n$k>l9g!"O"HV$,@hF,$KMhe(B
e$B$l$J$$$H$$$&[email protected])Ls$N$h$&$J5$$,$7$^$9!#e(B

e$B$($’$H!"$?$H$($Pe(B 0e$B$+$iL58B$KO"HV$r:n$ke(B iota e$B$He(B
e$BM-8B$NG[Nse(B ary e$B$,$"$C$?$H$7$F!"e(B
ary.each.with_index {|e,i| } e$B$Oe(B
ary.zip(iota) {|e,i| } e$B$H<B8=$G$-$k$,!“e(B
iota.zip(ary) {|i,e| }
e$B$OL58B%k!<%W$K$J$C$F$7$^$&!”$H$$$&0UL#$G$9$+!)e(B

% ./ruby -e ’
iota = Enumerator.new {|y| i = 0; loop { y << i; i += 1 } }
ary = %w[a b c]
ary.zip(iota) {|x,y| p [x,y] }
iota.zip(ary) {|x,y| p [x,y] }

[“a”, 0]
[“b”, 1]
[“c”, 2]
[0, “a”]
[1, “b”]
[2, “c”]
[3, nil]
[4, nil]
[5, nil]

e$B$G$b!"[email protected])Ls!"$H$$$&$N$O$h$/$o$+$j$^$;$s!#e(B

e$B$I$&$;%V%m%C%/0z?t$G<u$1$H$k$J$i!"=gHV$O$I$&$K$G$b$J$k$N$G$Oe(B?
e$BO"[email protected],$KI=<($7$?$1$l$P!"e(B
ary.zip(iota) {|e,i| p [i,e] }
e$B$H$9$l$P2DG=$G$9$h$M!#e(B

[email protected]$r$7$?$H$$$&e(B [ruby-core:14738] e$B$+$i$N%9%l%C%I$rFI$`$H!“e(B
e$B%l%7!<%P$ND9$5$rM%@h$9$k$H%f!<%6$,7+$jJV$7$N=N;$rA$Y$k$,!“e(B
e$B:GC;$N$G;_$^$k$HA*$Y$J$$!”$H$$$&[email protected]$,;XE&$5$l$F$$$F!”$=$l$K$O6&46$r3P$($^$9!#e(B

e$BO"[email protected],$KCV$$$?J}$,$o$+$j$d$9$$!"$H$$$&$J$i$o$+$i$J$$$G$b$J$$$G$9$,!“e(B
[email protected]$,!”%f!<%6$,A*$Y$k$H$$$&[email protected]$r1[$($k$H$O46$8$^$;$s!#e(B

e$B9M$($?$N$O!"e(Binterlavee$BBP>]$KL58BNs$r4^$`>l9g$G$9!#$?$H$($Pe(B
with_indexe$B$re(Bflattene$B$7$?$h$&[email protected]$k;~$K!"Dd;_$7$J$$$N$Oe(B
e$B;H$$$K$/$+$m$&$H!#e(B

iota.interleave(ary) {|x| p x } e$B$,e(B
0, “a”, 1, “b”, 2, “c”, 3, 4, 5, …
e$B$H$J$k$N$O;H$$$K$/$$!"$H!#e(B

e$B$U$`!#$=$l$O$=$s$J5$$,$7$J$$$G$b$J$$$G$9$M!#e(B
zip e$B$H$O0c$C$F!"%V%m%C%/0z?t$G$I$&$K$+$9$k$3$H$O$G$-$^$;$s$+$i$M!#e(B

e$B$G$b!“Dd;_$7$?$H$7$F$b!”$=$l$r$I$&;H$&$+$H$$$&$N$r9M$($k$He(B
e$B7k6I$O;H$$$K$/$$$H$$$&[email protected]$K$J$k$+$b$7$l$J$$$H$b;W$$$^$9!#e(B

e$B$I$&$b!"6qBNE*$K$J$j$-$l$J$$$N$,$$$1$^$;$s$M!#e(B

e$B$=$&$G$9$M!#6qBNNc$,$"$l$P!"$=$NNc$K$D$$$FJXMx$+$I$&$+$r9M$($i$l$k$s$G$9$,!"e(B
e$B$3$3$G=P$F$-$F$$$kNc$/[email protected]$H!"[email protected]$,B-$j$J$/$F$=$&$$$&$3$H$r9M$($k$N$Oe(B
e$BFq$7$$$G$9!#e(B

Hash[k1,v1,k2,v2,…] e$B$Oe(B
ks = [k1,k2,…]
vs = [v1,v2,…]
e$B$H$9$k$He(B Hash[*ks.interleave(vs)] e$B$H=q$1$F!"e(B
interleave e$B$,M-MQ$JNc$N$h$&$J5$$,$9$k$s$G$9$,!"e(B
e$B$3$l<+BN$OB>$NJ}K!$G2r7h$5$l$F$$$k$7!"e(B
e$BF1$8$h$&$J:$$jJ}$r$7$?B>$NNc$O;W$$=P$;[email protected]$h$J$!!#e(B

2010e$BG/e(B3e$B7ne(B14e$BF|e(B7:30 Yukihiro M.
[email protected]:

where interleave :: a -> [a] -> [[a]]
interleave x [] = [[x]]
interleave x (y:ys) = [x:y:ys] ++ map (y:) (interleave x ys)

e$B$O$+$i$:$be(Binterleavee$B$N%f!<%9%1!<%9$K$J$C$F$k$N$+$b$7$l$^$;$s!#e(B

e$B$3$Ne(B interleave e$B$O$=$Ne(B interleave
e$B$G$O$J$$$h$&$J5$$,$7$^$9!#e(B

% cat interleave.hs
interleave :: a -> [a] -> [[a]]
interleave x [] = [[x]]
interleave x (y:ys) = [x:y:ys] ++ map (y:) (interleave x ys)
% ghci interleave.hs
GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help
Loading package base … linking … done.
[1 of 1] Compiling Main ( interleave.hs, interpreted )
Ok, modules loaded: Main.
*Main> interleave 1 [2,3,4]
[[1,2,3,4],[2,1,3,4],[2,3,1,4],[2,3,4,1]]

e$B7?$be(B a -> [a] -> [[a]] e$B$G$9$,!“e(B
e$B:#2s$Ne(B interleave e$B$J$ie(B [a] -> [[a]] -> [a]
e$B$”$?$j$G$7$g$&!#e(B

|e$B$=$l$J$ie(B join e$B$r;H$($h!"$H$$$&5$$b$7$^$9$,!“e(Bjoin e$B$HHf3S$9$k$He(B
|* e$B:G8e$NMWAG$N=$o$j$K$b$D$1$i$l$ke(B
|
[email protected]$j$rJ#?t<o$K$G$-$ke(B
|* e$BJ8;zNs0J30$K$b;H$($ke(B
|e$B$H$$$&$”$?$j$,[email protected]$K$J$k$G$7$g$&$+!#e(B

e$B$"$H!“e(Bjoine$B$O$/$C$D$1$FJ8;zNs$K$7$F$7$^$&$N$G!”$=$l$,$&$l$7$$e(B
e$B;~$H!"$&$l$7$/$J$$;~$,$"$k$G$7$g$&$M!#e(B

e$BJ8;zNs0J30$K$b;H$($k$H$$$&9`L$G;XE&$7$?$D$b$j$G$7$?$,!“e(B
e$B$=$&$$$&MQES$b8!:w$9$k$H8+$D$+$j$^$9!#e(B
cond e$B$NCf?H$r:n$k$H$+!”%^%/%m4X78$,L\N)$D5$$,$7$^$9$,!#e(B
http://paste.lisp.org/display/93626
http://s-expressions.com/2010/01/24/conjure-simple-mocking-and-stubbing-for-clojure-unit-tests/

[email protected]$HHf3SBP>]$Oe(B Enumerable#zip e$B$de(B Array#transpose
e$B$"$?$j$G$7$g$&$+!#e(B
flatten(1) e$B$b9T$&$+$i!"%M%9%H$7$?$b$N$r07$o$J$/$F:Q$$V$se(B e$BF,$r;H$o$J$/$F:Q$$H$$$&[email protected]$O$"$k$+$J$!!#e(B

e$B$"$!!“e(BHash[k,v,k,v,…] [email protected]@.$Ke(B… e$B$C$F$3$l$O$9$G$Ke(B
Hash[[[k,v],[k,v],…]] e$B$G2r7h$5$l$F$k$+!#e(B
e$B$b$A$m$sJ#?t$N2r7h$,$”$C$F$b$$$$$o$1$G$9$,!#e(B

e$B:#2s$N<BAu$G$O!"0lHVC;$$e(BEnumerablee$B$+$i$NMWAG$r;H$$$-$C$?;[email protected](B
e$B$GDd;_$7$^$9!#e(BClojuree$B$b$=$&$$$&5sF0$G$7$?!#e(B

e$B$J$k$[$I!#e(B

zip e$B$G0JA0!"$=$N$"$?$j$,JQ$o$C$FLa$C$?$h$&$J5-21$,$"$k$s$G$9$,!"e(B
e$B$I$&$G$7$?$C$1e(B?

http://code.activestate.com/recipes/511480-interleaving-sequences/

e$B$G>R2p$5$l$F$$$ke(BPythone$BHG$O!"$J$<$+;D$C$?$b$N$r<h$j=P$7B3$1$ke(B
e$B5sF0$K$J$C$F$$$^$9$,!"$3$l$O;H$$$K$/$$$s$8$c$J$$$+$J$"!#e(B

e$B$=$3$GF,$NCf$KIb$+$s$G$$$kMQES$r=q$/$N$G$9!#e(B

e$B$=$l$H$bK\Ev$KMQES$r;W$$Ib$+$Y$:$K;H$$$K$/$$$H46$8$k$s$G$7$g$&$+!#e(B
e$B$=$&[email protected]$H$7$?$i=q$1$J$$$+$b$7$l$^$;$s$,!"$=$&$J$N$+$J$!!#e(B

e$B:XF#$H?=$7$^$9!#e(B

On Tue, 16 Mar 2010 17:59:48 +0900
Tanaka A. [email protected] wrote:

e$B$I$&$b!"6qBNE*$K$J$j$-$l$J$$$N$,$$$1$^$;$s$M!#e(B

e$B$=$&$G$9$M!#6qBNNc$,$"$l$P!"$=$NNc$K$D$$$FJXMx$+$I$&$+$r9M$($i$l$k$s$G$9$,!"e(B
e$B$3$3$G=P$F$-$F$$$kNc$/[email protected]$H!"[email protected]$,B-$j$J$/$F$=$&$$$&$3$H$r9M$($k$N$Oe(B
e$BFq$7$$$G$9!#e(B

e$BK\20$5$s$G!“8"0R$,$”$j$=$&$J%W%m%0%i%%s%0e(BClojuree$B$r8+$D$1$?$N$G!"e(Binterleavee$B$Ne(B
e$B<BMQE*$JNc$,:$C$F$k$s$8$c$J$$$+$H4|BT$7$FN)$AFI$
$7$F$_$^$7$?!#e(B

e$B$G$9$,K$K:$C$F$$$?$N$O!"!V%j%9%H$K$7$?J8;zNs$K4JC1$J0E9f2=$r;$9!W$H$$$&$b$Ne(B
e$B$N$_$G$7$?!#$3$l$G$O<BMQE*[email protected]$$Fq$$$H46$8$^$9!#e(B

e$B$H$$$&;29M>pJs$H$H$b$K!"<+J,$bDI2C$NI,[email protected]$r$"$s$^$j46$8$J$$$N$G!"AG?M$J$,$ie(B
-1 e$B$H=q$+$;[email protected]$-$^$9!#e(B

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs