David Alan Black (dblack / candle.superlink.net) wrote:
: My current best shot at an interleave method is:

: class Array

:   def interleave(ary2)
:     a1, a2, res = self.dup, ary2.dup, []
:     res.push a1.shift, a2.shift until a1.empty? or a2.empty?
:     for n in [a1, a2]
:       res.concat n unless n.empty?
:     end
:     res
:   end

: end

A somewhat different approach I thought of is:

class Array
  def intereach(arr)
    length.times { |n| yield(at(n), arr.at(n)) }
  end
end

card = [1,2,3,4]
ord = ['first','second','third','fourth']

hs = [] 
card.intereach(ord) { |n, m| hs << n << m }

hs # => [1, "first", 2, "second", 3, "third", 4, "fourth"]

Which gets the desired result.

: which is about as fast as I can get it (non-destructively :-)  

I have no idea how fast it is, but hopefully it's at least worth investigating.



--
Cullen J O'Neill
--
cjon / engin.umich.edu