Ruby-dev summary 18613-18710

Hi –

[…]

There is a discussion whether Enumerable#zip is appropriate or not.

ex.

  • Enumerable.zip(a, b, c…)
  • Array.zip(a, b, c…)
  • [a, b, c…].zip

How about the latter, with an optional block.

a = [1, 0, 3]
b = [2, 1, 2]
c = [3, 2, 1]

[a, b, c].zip { |x, y, z| x*y + z } #=> [5, 2, 7]

Now zip is like lisp’s map.

The optional block is cool, but I don’t know about having the return
value of the method being different depending on the presence or
absence of a block. I’m not sure, though.

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 }

but the actual return value of the method stayed the same. (Not that
my #braid method is a model of Ruby perfection – the extant version
seems to have a rogue concat where a << should be… :slight_smile: So it was
more like #each than #map, or something. I don’t know whether there’s
a real underlying reason to do it this way.

David

···

On Fri, 15 Nov 2002, Tim Sutherland wrote:

In article 20021112020739J.maki@rubycolor.org, TAKAHASHI Masayoshi wrote:


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

Hi –

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 :slight_smile:
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

···

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


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

LOL-I posted an almost-carbon-copy earlier today, even in the choice
of some words and idioms. Good f’ me, it must mean I’m getting good
at last… :-)))

/me resurrects thoughts about snippet/extended builtins repository…

Massimiliano

···

On Fri, Nov 15, 2002 at 12:20:41PM +0900, dblack@candle.superlink.net wrote:

class Array
def braid2(*others)
all = [self, *others]

Hi –

···

On Sat, 16 Nov 2002, Massimiliano Mirra wrote:

/me resurrects thoughts about snippet/extended builtins repository…

That is never far from my thoughts either. We need it now as much as
ever – though I still think the key to it is some kind of mechanism
for using the snippets and extensions in a safe way.

David


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