Hi --

On Fri, 15 Nov 2002 dblack / candle.superlink.net wrote:

> Many moons ago I wrote Array#braid, which was essentially a
> n-dimensional 'zip'.  It returned a "braided" version of the input
> arrays:
>
>   a.braid(b,c) # => [ [1,2,3], [0,1,2], [3,2,1] ]
>
> and it could take a block:
>
>   res = []
>   a.braid(b,c) { |x,y,z| res << x*y+z }

If anyone wants to play with this, here's a dusted off and slightly
neatened up version:

  class Array
    def braid(*others)
      all = [self, *others]
      (0...all.map {|a| a.size}.max).map do |i|
	column = all.map {|a| a[i]}
	yield column if block_given?
	column
      end
    end
    alias :zip :braid     # if you want :-)
  end

Note that you can change it to the behavior Tim was talking about
(return value taken from the block if there's a block) like this:

  class Array
    def braid2(*others)
      all = [self, *others]
      (0...all.map {|a| a.size}.max).map do |i|
	column = all.map {|a| a[i]}
	if block_given? then yield(column) else column end
      end
    end
    alias :zip :braid
  end


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav