# Request for two methods in Array class

**URL:** <https://rubytalk.org/t/request-for-two-methods-in-array-class/12862>\
**Category:** ruby-talk\
**Created:** [2 August 2004 19:26 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862 "2004-08-02T19:26:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Hall4](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Mike\_Hall4](https://rubytalk.org/u/Mike_Hall4)\
**Post date:** [2 August 2004 19:26 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/1 "2004-08-02T19:26:34Z")

</div>

Two small requests:

1. A new method, Array.combine (needs a better name for general use).  
&nbsp;&nbsp;&nbsp;It takes entries from two (or many) arrays and combines them.  
&nbsp;&nbsp;&nbsp;(like a combination of Array.zip, fetch, and map)

&nbsp;&nbsp;&nbsp;For my use, I needed multiplication, but a block would be more general.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = [1,2,3]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b = [1,3,5]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.combine(b) =\> [1,6,15]

&nbsp;&nbsp;&nbsp;Or with a block:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.combine(b) {|x,y| x\*y}  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;The new method would replace this sequence:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.zip(b).map {|e| e[0]\*e[1]}  
&nbsp;&nbsp;&nbsp;&nbsp;or  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.zip(b,c,d).map {|e| e.inject(1) {|prod, n| prod\*n} }

2. A block initializer for Array.new, like that for Hash.new.  
&nbsp;&nbsp;&nbsp;Good for self-initializing a table of recurrence relations.  
&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;a = Array.new() {|self, i| 2\*self[i-1] - self[i-2]}

&nbsp;&nbsp;&nbsp;&nbsp;a[0] = 1 # define the first two ...  
&nbsp;&nbsp;&nbsp;&nbsp;a[1] = x

&nbsp;&nbsp;&nbsp;&nbsp;a[2] =\> 2x-1 # the rest are derived on the fly as needed  
&nbsp;&nbsp;&nbsp;&nbsp;a[3] =\> ...

Hopefully, I'm not blind and haven't missed some obvious use  
of the existing methods. My apologies if that's the case.

Whatever... just some thoughts. Are they worth adding?

Thanks!

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Dell1](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@Sean\_O\_Dell1](https://rubytalk.org/u/Sean_O_Dell1)\
**Post date:** [2 August 2004 19:34 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/2 "2004-08-02T19:34:25Z")

</div>

Or perhaps:

c = a.weave(b)  
c #=\> [[1, 1], [2, 3], [3, 5]]

...then you could:

c.collect! do {|item| item[0] \* item[1]}

c #=\> [1, 6, 15]

&nbsp;&nbsp;Sean O'Dell

> **···**
>
> On Monday 02 August 2004 12:26, Mike Hall wrote:
> 
> > 1. A new method, Array.combine (needs a better name for general use).  
> > &nbsp;&nbsp;&nbsp;It takes entries from two (or many) arrays and combines them.  
> > &nbsp;&nbsp;&nbsp;(like a combination of Array.zip, fetch, and map)
> > 
> > &nbsp;&nbsp;&nbsp;For my use, I needed multiplication, but a block would be more general.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = [1,2,3]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b = [1,3,5]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.combine(b) =\> [1,6,15]
> > 
> > &nbsp;&nbsp;&nbsp;Or with a block:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.combine(b) {|x,y| x\*y}

---

<div class="post-metadata">

**Author:** ![George5](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@George5](https://rubytalk.org/u/George5)\
**Post date:** [2 August 2004 19:45 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/3 "2004-08-02T19:45:10Z")

</div>

Mike Hall wrote:

> Two small requests:
> 
> 1. A new method, Array.combine (needs a better name for general use).  
> &nbsp;&nbsp;&nbsp;It takes entries from two (or many) arrays and combines them.  
> &nbsp;&nbsp;&nbsp;(like a combination of Array.zip, fetch, and map)
> 
> &nbsp;&nbsp;&nbsp;For my use, I needed multiplication, but a block would be more general.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = [1,2,3]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b = [1,3,5]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.combine(b) =\> [1,6,15]
> 
> &nbsp;&nbsp;&nbsp;Or with a block:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.combine(b) {|x,y| x\*y}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The new method would replace this sequence:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.zip(b).map {|e| e[0]\*e[1]}

It's the same length:

&nbsp;&nbsp;a.zip(b).map {|x,y| x\*y}  
&nbsp;&nbsp;# vs.  
&nbsp;&nbsp;a.combine(b) {|x,y| x\*y}

But I must say, that I like the name combine a lot.

Regards,

&nbsp;&nbsp;&nbsp;Michael

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [3 August 2004 05:26 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/4 "2004-08-03T05:26:34Z")

</div>

Hi,

> 1. A new method, Array.combine (needs a better name for general use).  
> &nbsp;&nbsp;It takes entries from two (or many) arrays and combines them.  
> &nbsp;&nbsp;(like a combination of Array.zip, fetch, and map)

Sounds nice. But I'm not sure "combine" is a proper name for the  
method. We need more discussion, for example:

&nbsp;&nbsp;\* the best name for the behavior.  
&nbsp;&nbsp;\* whether it takes only one argument or any number of arguments.  
&nbsp;&nbsp;\* whether "multiplication" should be the default.

> 2. A block initializer for Array.new, like that for Hash.new.  
> &nbsp;&nbsp;Good for self-initializing a table of recurrence relations.

Array.new already takes a block, with little bit different behavior  
than what you've describe. In addition,

> &nbsp;&nbsp;&nbsp;a = Array.new() {|self, i| 2\*self[i-1] - self[i-2]}

I assume this accesses self[-1] and self[-2] during the array  
initialization, which seem no good. Perhaps there should be better  
way to express what you want.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

> **···**
>
> In message "Request for two methods in Array class" on 04/08/03, Mike Hall \<mghallNO@SPAMenteract.com\> writes:

---

<div class="post-metadata">

**Author:** ![Caio\_Chassot1](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@Caio\_Chassot1](https://rubytalk.org/u/Caio_Chassot1)\
**Post date:** [3 August 2004 09:38 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/5 "2004-08-03T09:38:37Z")

</div>

Mike Hall wrote:

> 1. A new method, Array.combine (needs a better name for general use).  
> &nbsp;&nbsp;&nbsp;It takes entries from two (or many) arrays and combines them.  
> &nbsp;&nbsp;&nbsp;(like a combination of Array.zip, fetch, and map)

Following on my suggestion that combine is just an extension of map, how do you feel about this code?:

class Array  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alias :\_\_map :map  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def map \*args, &block  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;identity = proc{ |\*items| items.length==1 ? items[0] : items }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.zip(\*args).\_\_map{ |line| (block or identity).call \*line }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alias :collect :map  
end

irb(main):003:0\> [1,2,3].map  
=\> [1, 2, 3]  
irb(main):004:0\> [1,2,3].map [1,3,5]  
=\> [[1, 1], [2, 3], [3, 5]]  
irb(main):005:0\> [1,2,3].map([1,3,5]){|x,y|x\*y}  
=\> [1, 6, 15]

---

<div class="post-metadata">

**Author:** ![Sam\_McCall](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@Sam\_McCall](https://rubytalk.org/u/Sam_McCall)\
**Post date:** [3 August 2004 12:26 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/6 "2004-08-03T12:26:45Z")

</div>

For "combine":  
How much of a performance problem would it be to have zip, if given a block, return an array of the results, instead of nil? Code using it for side-effects at the moment would be unaffected (but it would create a potentially big array). Then you could do  
[1,2,3].zip([4,5,6]) # =\> [[1,4],[2,5],[3,6]]

[1,2,3].zip([4,5,6]) { |x,y| puts x\*y }  
\> 4  
\> 10  
\> 18  
# =\> [nil,nil,nil] (this would presumably be ignored)

[1,2,3].zip([4,5,6]) { |x,y| x\*y } # =\> [4,10,18]

This generalises to more than one array...

Sam

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard3](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@Ara.T.Howard3](https://rubytalk.org/u/Ara.T.Howard3)\
**Post date:** [2 August 2004 20:21 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/7 "2004-08-02T20:21:34Z")

</div>

> It's the same length:  
> a.zip(b).map {|x,y| x\*y}  
> # vs.  
> a.combine(b) {|x,y| x\*y}

but one less object creation

> But I must say, that I like the name combine a lot.

ditto.

-a

> **···**
>
> # On Tue, 3 Aug 2004, Michael Neumann wrote: --
> 
> > EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov  
> > PHONE :: 303.497.6469  
> > A flower falls, even though we love it;  
> > and a weed grows, even though we do not love it. --Dogen
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Caio\_Chassot1](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@Caio\_Chassot1](https://rubytalk.org/u/Caio_Chassot1)\
**Post date:** [3 August 2004 05:59 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/8 "2004-08-03T05:59:09Z")

</div>

Yukihiro Matsumoto wrote:

> \>1. A new method, Array.combine (needs a better name for general use).  
> \> It takes entries from two (or many) arrays and combines them.  
> \> (like a combination of Array.zip, fetch, and map)

I don't think we need another method for this. This sounds like the domain of Enumerable#map (at least as far as how map works in lisp and python).

Is there anything about map that prevents it from being extended to accept 0 or more enumerables as parameters, then a block?

According to this rationale:

> &nbsp;&nbsp;\* the best name for the behavior.

map

> &nbsp;&nbsp;\* whether it takes only one argument or any number of arguments.

0 or more. (Much more powerful, and we don't have to give up anything about only taking one arg to support taking many)

> &nbsp;&nbsp;\* whether "multiplication" should be the default.

If by "default" you mean "when no block is passed", as I see it, the default should be identical or similar to zip.

in python, map with multiple lists and no function returns a list with the length of the largest list, each element being a list of the elements at its index in the lists passed in, None is used for elements that don't exist in the other lists.

I like python's behavior, but I think in the case of ruby, using the length of the receiver better matches expected behavior than using a length from one of the parameters.

(on the other hand, using the longest length makes it more useful in that it differs from zip, and it seems more useful in most cases)

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [3 August 2004 08:56 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/9 "2004-08-03T08:56:33Z")

</div>

"Yukihiro Matsumoto" \<matz@ruby-lang.org\> schrieb im Newsbeitrag  
news:1091510011.625923.536.nullmailer@picachu.netlab.jp...

> Hi,
> 
> \>1. A new method, Array.combine (needs a better name for general use).  
> \> It takes entries from two (or many) arrays and combines them.  
> \> (like a combination of Array.zip, fetch, and map)
> 
> Sounds nice. But I'm not sure "combine" is a proper name for the  
> method. We need more discussion, for example:
> 
> &nbsp;&nbsp;\* the best name for the behavior.

I like "combine" but I'm open to suggestions.

> &nbsp;&nbsp;\* whether it takes only one argument or any number of arguments.

IMHO n\>=0

> &nbsp;&nbsp;\* whether "multiplication" should be the default.

No IMHO.

I imageine behavior similar to this:

class Array  
&nbsp;&nbsp;def combine(\*arrays, &b)  
&nbsp;&nbsp;&nbsp;&nbsp;result =   
&nbsp;&nbsp;&nbsp;&nbsp;each\_with\_index {|e,idx| result \<\< b.call(e, \*arrays.map {|ar|  
ar[idx]})}  
&nbsp;&nbsp;&nbsp;&nbsp;result  
&nbsp;&nbsp;end  
end

Note that arrays can contain anything that implements , which might even  
be a lambda (aka function):

a = [1,2,3]  
a.combine(lambda {|i| i\*2}) {|x,y| x\*y}  
=\> [0, 4, 12]

So "arrays" might not be the ideal argument name.

Thinking a bit further about this it seems to me that the method might be  
better put into Enumerable or Array as module method since arguments seem  
to be symmetric:

class Array  
&nbsp;&nbsp;def self.combine(\*arrays, &b)  
&nbsp;&nbsp;&nbsp;&nbsp;limit = arrays.inject(0){|l,a| s=a.size; s\>l ? s : l}  
&nbsp;&nbsp;&nbsp;&nbsp;result = Array.new limit  
&nbsp;&nbsp;&nbsp;&nbsp;limit.times {|i| result[i] = b.call(\*arrays.map {|ar| ar[i]}) }  
&nbsp;&nbsp;&nbsp;&nbsp;result  
&nbsp;&nbsp;end  
end

irb(main):054:0\> Array.combine([1,2,3],[1,3,5]) {|x,y| x\*y}  
=\> [1, 6, 15]  
irb(main):055:0\> Array.combine([1,2,3],[1,3,5]) {|\*x| x.inject(1){|z,i|  
z\*i} }  
=\> [1, 6, 15]

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > In message "Request for two methods in Array class" \> on 04/08/03, Mike Hall \<mghallNO@SPAMenteract.com\> writes:

---

<div class="post-metadata">

**Author:** ![George5](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@George5](https://rubytalk.org/u/George5)\
**Post date:** [3 August 2004 09:32 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/10 "2004-08-03T09:32:31Z")

</div>

Yukihiro Matsumoto wrote:

> Hi,
> 
> \>1. A new method, Array.combine (needs a better name for general use).  
> \> It takes entries from two (or many) arrays and combines them.  
> \> (like a combination of Array.zip, fetch, and map)
> 
> Sounds nice. But I'm not sure "combine" is a proper name for the  
> method. We need more discussion, for example:
> 
> &nbsp;&nbsp;\* the best name for the behavior.

combine sounds nice to me. it "combinates" two or more arrays (or enumerables) into one. but one might argue against combine as name, as it it's not clear that combine returns an array that is not larger than it's arguments joined together.

IMO, combine should be equal to zip.map:

&nbsp;&nbsp;&nbsp;enum.combine(enum2, enum3,...) { block }  
&nbsp;&nbsp;&nbsp;# equal  
&nbsp;&nbsp;&nbsp;enum.zip(enum2, enum3,...).map { block }

> &nbsp;&nbsp;\* whether it takes only one argument or any number of arguments.

many

> &nbsp;&nbsp;\* whether "multiplication" should be the default.

no. default should be "{|arr| arr}" (the default of map)

Regards,

&nbsp;&nbsp;&nbsp;Michael

> **···**
>
> > In message "Request for two methods in Array class" \> on 04/08/03, Mike Hall \<mghallNO@SPAMenteract.com\> writes:

---

<div class="post-metadata">

**Author:** ![Christoph\_Neubauer](https://avatars.discourse-cdn.com/v4/letter/c/b782af/32.png) [@Christoph\_Neubauer](https://rubytalk.org/u/Christoph_Neubauer)\
**Post date:** [3 August 2004 09:36 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/11 "2004-08-03T09:36:35Z")

</div>

"Yukihiro Matsumoto" \<matz@ruby-lang.org\> wrote in message  
news:1091510011.625923.536.nullmailer@picachu.netlab.jp...

> Hi,
> 
> \>1. A new method, Array.combine (needs a better name for general use).  
> \> It takes entries from two (or many) arrays and combines them.  
> \> (like a combination of Array.zip, fetch, and map)
> 
> Sounds nice. But I'm not sure "combine" is a proper name for the  
> method. We need more discussion, for example:

Hi Matz !

I'm using Ruby 1.6.8.  
Maybe my suggestions are already part of some later version,  
or can be upgraded to some more fittable behaviour.

> &nbsp;&nbsp;\* the best name for the behavior.

--\> match

> &nbsp;&nbsp;\* whether it takes only one argument or any number of arguments.

--\> one or more

> &nbsp;&nbsp;\* whether "multiplication" should be the default.

If no (or an empty) block is given, what would be the most common way to  
'match' array elements ?  
Regarding to Sean O'Dells 'weave example', I think it's like this:

natural = [1,2,3]  
odd = [1,3,5]  
even = [2,4,6]

matched = natural.match(odd,even)  
matched #=\> [[1, 1, 2], [2, 3, 4], [3, 5, 6]]

Greetings,  
Chris

\<snip\>

> **···**
>
> > In message "Request for two methods in Array class" \> on 04/08/03, Mike Hall \<mghallNO@SPAMenteract.com\> writes:

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [3 August 2004 11:11 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/12 "2004-08-03T11:11:36Z")

</div>

> Hi,
> 
> \>1. A new method, Array.combine (needs a better name for general use).  
> \> It takes entries from two (or many) arrays and combines them.  
> \> (like a combination of Array.zip, fetch, and map)
> 
> Sounds nice. But I'm not sure "combine" is a proper name for the  
> method. We need more discussion, for example:
> 
> &nbsp;&nbsp;\* the best name for the behavior.  
> &nbsp;&nbsp;\* whether it takes only one argument or any number of arguments.

Any number, I'd say

> &nbsp;&nbsp;\* whether "multiplication" should be the default.

Almost certainly not.

Getting a bit complicated, but what I'd like is to check if the last or  
two last args are a symbol and an optional seed, which are injected  
across the current tuple.

e.g. the multiplication case would be a.combine(b,c,d, :\*) which would mean  
a.zip(b,c,d) {|\*args| args.inject {|a,v| a.send(:\*, v)}}

If both a symbol and a block are provided, the symbol is injected first  
and the result passed to the block.

Default (no symbol and no block) should be to just zip the arrays.

martin

> **···**
>
> Yukihiro Matsumoto \<matz@ruby-lang.org\> wrote:
> 
> > In message "Request for two methods in Array class" \> on 04/08/03, Mike Hall \<mghallNO@SPAMenteract.com\> writes:

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [3 August 2004 12:59 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/13 "2004-08-03T12:59:57Z")

</div>

Hi --

> Hi,
> 
> \>1. A new method, Array.combine (needs a better name for general use).  
> \> It takes entries from two (or many) arrays and combines them.  
> \> (like a combination of Array.zip, fetch, and map)
> 
> Sounds nice. But I'm not sure "combine" is a proper name for the  
> method. We need more discussion, for example:

I know I'm being the voice of possibly excessive conservatism  
here... but I'm just wondering what this method would give us, since  
we can already do zip+map. As Ara said, it would create one less  
object. But chaining methods always creates intermediate objects. If  
zip+map deserves to be combined into a new, single method, what about  
flatten+map, or compact+map? And then (flatten+map)+compact....

I realize that zip+map is more likely to be actually used, and people  
have often said they wish zip's block mapped the elements. But I  
still wonder whether it's worth a new method and method name.

David

> **···**
>
> On Tue, 3 Aug 2004, Yukihiro Matsumoto wrote:
> 
> > In message "Request for two methods in Array class" \> on 04/08/03, Mike Hall \<mghallNO@SPAMenteract.com\> writes:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Charles\_Mills1](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@Charles\_Mills1](https://rubytalk.org/u/Charles_Mills1)\
**Post date:** [3 August 2004 01:51 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/14 "2004-08-03T01:51:34Z")

</div>

> > It's the same length:  
> > a.zip(b).map {|x,y| x\*y}  
> > # vs.  
> > a.combine(b) {|x,y| x\*y}
> 
> but one less object creation
> 
> > But I must say, that I like the name combine a lot.
> 
> ditto.

Seems like a good idea. After reading the chapter of Meyers book Lothar sent, the idea seems even better.  
Another name idea is collect\_wtih  
c = a.collect\_with(b) { |x,y| x\*y }  
a.collect\_with!(b) { |x,y| x\*y } # looks bad?  
d = a.collect\_with(b, c) { |x,y,z| x\*y\*z } # multiple params would be useful  
-Charlie

> **···**
>
> On Aug 2, 2004, at 1:21 PM, Ara.T.Howard wrote:
> 
> > On Tue, 3 Aug 2004, Michael Neumann wrote:
> 
> > # -a --
> > 
> > # \> EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov \> PHONE :: 303.497.6469 \> A flower falls, even though we love it; \> and a weed grows, even though we do not love it. | --Dogen

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [3 August 2004 08:31 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/15 "2004-08-03T08:31:36Z")

</div>

"Ara.T.Howard" \<ahoward@noaa.gov\> schrieb im Newsbeitrag  
news:Pine.LNX.4.60.0408021412540.11770@harp.ngdc.noaa.gov...

> **···**
>
> > On Tue, 3 Aug 2004, Michael Neumann wrote:
> > 
> > \> It's the same length:  
> > \> a.zip(b).map {|x,y| x\*y}  
> > \> # vs.  
> > \> a.combine(b) {|x,y| x\*y}
> > 
> > but one less object creation
> 
> You can save that by using inject:
> 
> a = [1,2,3]  
> b = [1,3,5]  
> a.inject(){|arr,x| arr \<\< (x\*b[arr.size])}
> 
> Regards
> 
> &nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [3 August 2004 13:08 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/16 "2004-08-03T13:08:29Z")

</div>

That's good thinking. The most compelling examples so far have been  
the ones where #zip actually has map-like functionality. No need for  
a new method. \*Definitely\* don't touch #map !!

&nbsp;&nbsp;arr.zip { ... } # same behaviour as arr.map { ... }  
&nbsp;&nbsp;arr.zip(x) { ... } # " " " " arr.zip(x).map { ... }  
&nbsp;&nbsp;etc.  
&nbsp;&nbsp;  
Gavin

> **···**
>
> On Tuesday, August 3, 2004, 10:59:57 PM, David wrote:
> 
> > Hi --
> 
> > On Tue, 3 Aug 2004, Yukihiro Matsumoto wrote:
> 
> > > Hi,
> > > 
> > > In message "Request for two methods in Array class" \>\> on 04/08/03, Mike Hall \<mghallNO@SPAMenteract.com\> writes:
> > > 
> > > \>1. A new method, Array.combine (needs a better name for general use).  
> > > \> It takes entries from two (or many) arrays and combines them.  
> > > \> (like a combination of Array.zip, fetch, and map)
> > > 
> > > Sounds nice. But I'm not sure "combine" is a proper name for the  
> > > method. We need more discussion, for example:
> 
> > I know I'm being the voice of possibly excessive conservatism  
> > here... but I'm just wondering what this method would give us, since  
> > we can already do zip+map. As Ara said, it would create one less  
> > object. But chaining methods always creates intermediate objects. If  
> > zip+map deserves to be combined into a new, single method, what about  
> > flatten+map, or compact+map? And then (flatten+map)+compact....
> 
> > I realize that zip+map is more likely to be actually used, and people  
> > have often said they wish zip's block mapped the elements. But I  
> > still wonder whether it's worth a new method and method name.

---

<div class="post-metadata">

**Author:** ![Claus\_Spitzer](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@Claus\_Spitzer](https://rubytalk.org/u/Claus_Spitzer)\
**Post date:** [3 August 2004 15:19 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/17 "2004-08-03T15:19:10Z")

</div>

How about (snipped from Sean's e-mail earlier)

a.weave(b)  
#=\> [[1, 1], [2, 3], [3, 5]]

The default action would be to create the larger array, but a block  
could be used to prevent the array from growing, aka

a.weave(b) {|x,y| x\*y}  
#=\> [1, 6, 15]

> **···**
>
> On Tue, 3 Aug 2004 18:32:31 +0900, Michael Neumann \<mneumann@ntecs.de\> wrote:
> 
> > Yukihiro Matsumoto wrote:  
> > \> Hi,  
> > \>  
> > \> In message "Request for two methods in Array class" \> \> on 04/08/03, Mike Hall \<mghallNO@SPAMenteract.com\> writes:  
> > \>  
> > \> \>1. A new method, Array.combine (needs a better name for general use).  
> > \> \> It takes entries from two (or many) arrays and combines them.  
> > \> \> (like a combination of Array.zip, fetch, and map)  
> > \>  
> > \> Sounds nice. But I'm not sure "combine" is a proper name for the  
> > \> method. We need more discussion, for example:  
> > \>  
> > \> \* the best name for the behavior.
> > 
> > combine sounds nice to me. it "combinates" two or more arrays (or  
> > enumerables) into one. but one might argue against combine as name, as  
> > it it's not clear that combine returns an array that is not larger than  
> > it's arguments joined together.
> > 
> > IMO, combine should be equal to zip.map:
> > 
> > &nbsp;&nbsp;&nbsp;enum.combine(enum2, enum3,...) { block }  
> > &nbsp;&nbsp;&nbsp;# equal  
> > &nbsp;&nbsp;&nbsp;enum.zip(enum2, enum3,...).map { block }
> > 
> > \> \* whether it takes only one argument or any number of arguments.
> > 
> > many
> > 
> > \> \* whether "multiplication" should be the default.
> > 
> > no. default should be "{|arr| arr}" (the default of map)
> > 
> > Regards,
> > 
> > &nbsp;&nbsp;&nbsp;Michael

---

<div class="post-metadata">

**Author:** ![Claus\_Spitzer](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@Claus\_Spitzer](https://rubytalk.org/u/Claus_Spitzer)\
**Post date:** [3 August 2004 15:28 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/18 "2004-08-03T15:28:12Z")

</div>

That's a strong point. Elaborating a bit more on that, perhaps what is  
needed is not a new method (or a collection of them), but a way of  
piping objects instead of creating intermediate ones. Then methods  
could be still chained, but with better spatial efficiency.  
&nbsp;&nbsp;&nbsp;-CWS

> **···**
>
> On Tue, 3 Aug 2004 21:59:57 +0900, David A. Black \<dblack@wobblini.net\> wrote:
> 
> > Hi --
> > 
> > On Tue, 3 Aug 2004, Yukihiro Matsumoto wrote:
> > 
> > \> Hi,  
> > \>  
> > \> In message "Request for two methods in Array class" \> \> on 04/08/03, Mike Hall \<mghallNO@SPAMenteract.com\> writes:  
> > \>  
> > \> \>1. A new method, Array.combine (needs a better name for general use).  
> > \> \> It takes entries from two (or many) arrays and combines them.  
> > \> \> (like a combination of Array.zip, fetch, and map)  
> > \>  
> > \> Sounds nice. But I'm not sure "combine" is a proper name for the  
> > \> method. We need more discussion, for example:
> > 
> > I know I'm being the voice of possibly excessive conservatism  
> > here... but I'm just wondering what this method would give us, since  
> > we can already do zip+map. As Ara said, it would create one less  
> > object. But chaining methods always creates intermediate objects. If  
> > zip+map deserves to be combined into a new, single method, what about  
> > flatten+map, or compact+map? And then (flatten+map)+compact....
> > 
> > I realize that zip+map is more likely to be actually used, and people  
> > have often said they wish zip's block mapped the elements. But I  
> > still wonder whether it's worth a new method and method name.
> > 
> > David
> > 
> > --  
> > David A. Black  
> > dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Charles\_Mills1](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@Charles\_Mills1](https://rubytalk.org/u/Charles_Mills1)\
**Post date:** [3 August 2004 02:32 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/19 "2004-08-03T02:32:35Z")

</div>

> > > It's the same length:  
> > > a.zip(b).map {|x,y| x\*y}  
> > > # vs.  
> > > a.combine(b) {|x,y| x\*y}
> > 
> > but one less object creation
> > 
> > > But I must say, that I like the name combine a lot.
> > 
> > ditto.
> 
> Seems like a good idea. After reading the chapter of Meyers book Lothar sent, the idea seems even better.  
> Another name idea is collect\_wtih  
> c = a.collect\_with(b) { |x,y| x\*y }  
> a.collect\_with!(b) { |x,y| x\*y } # looks bad?  
> d = a.collect\_with(b, c) { |x,y,z| x\*y\*z } # multiple params would be useful

Looks like this has been talked about by others:  
[http://www.ai.mit.edu/~gregs/ll1-discuss-archive-html/msg03386.html](http://www.ai.mit.edu/~gregs/ll1-discuss-archive-html/msg03386.html)  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/29671](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/29671)

still seems useful to me though  
c = a.collect\_with(keys) { ... }

> **···**
>
> On Aug 2, 2004, at 6:51 PM, Charles Mills wrote:
> 
> > On Aug 2, 2004, at 1:21 PM, Ara.T.Howard wrote:
> > 
> > > On Tue, 3 Aug 2004, Michael Neumann wrote:
> > 
> > -Charlie
> > 
> > > # -a --
> > > 
> > > # \> EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov \> PHONE :: 303.497.6469 \> A flower falls, even though we love it; \> and a weed grows, even though we do not love it. | --Dogen

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [3 August 2004 13:32 UTC](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862/20 "2004-08-03T13:32:40Z")

</div>

Hi --

> **···**
>
> On Tue, 3 Aug 2004, Gavin Sinclair wrote:
> 
> > On Tuesday, August 3, 2004, 10:59:57 PM, David wrote:
> > 
> > \> Hi --
> > 
> > \> On Tue, 3 Aug 2004, Yukihiro Matsumoto wrote:
> > 
> > \>\> Hi,  
> > \>\>  
> > \>\> In message "Request for two methods in Array class" \> \>\> on 04/08/03, Mike Hall \<mghallNO@SPAMenteract.com\> writes:  
> > \>\>  
> > \>\> \>1. A new method, Array.combine (needs a better name for general use).  
> > \>\> \> It takes entries from two (or many) arrays and combines them.  
> > \>\> \> (like a combination of Array.zip, fetch, and map)  
> > \>\>  
> > \>\> Sounds nice. But I'm not sure "combine" is a proper name for the  
> > \>\> method. We need more discussion, for example:
> > 
> > \> I know I'm being the voice of possibly excessive conservatism  
> > \> here... but I'm just wondering what this method would give us, since  
> > \> we can already do zip+map. As Ara said, it would create one less  
> > \> object. But chaining methods always creates intermediate objects. If  
> > \> zip+map deserves to be combined into a new, single method, what about  
> > \> flatten+map, or compact+map? And then (flatten+map)+compact....
> > 
> > \> I realize that zip+map is more likely to be actually used, and people  
> > \> have often said they wish zip's block mapped the elements. But I  
> > \> still wonder whether it's worth a new method and method name.
> > 
> > That's good thinking. The most compelling examples so far have been  
> > the ones where #zip actually has map-like functionality. No need for  
> > a new method. \*Definitely\* don't touch #map !!
> > 
> > &nbsp;&nbsp;arr.zip { ... } # same behaviour as arr.map { ... }  
> > &nbsp;&nbsp;arr.zip(x) { ... } # " " " " arr.zip(x).map { ... }  
> > &nbsp;&nbsp;etc.
> 
> The problem with having zip do auto-mapping is that you lose the  
> ability to do zip with a block but without mapping (which might be  
> desireable for performance or other reasons). Whereas if it stays the  
> way it is, you can always map the results.
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

[Next page](https://rubytalk.org/t/request-for-two-methods-in-array-class/12862.md?page=2)
