Feature #5044


#zip with block return mapped results

Added by trans (Thomas Sawyer) about 11 years ago. Updated about 3 years ago.

Target version:


Is there any reason that #zip doesn't return results like map?

[1,2,3].zip([1,2,3]){ |a,b| a + b } #=> [2,4,6]

Currently, it just returns nil, which seems rather contrary to the return result of the non-block form.

Related issues 2 (1 open1 closed)

Related to Ruby master - Feature #4539: Array#zip_withAssignedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #16261: Enumerable#each_splat and Enumerator#splatRejectedActions

Updated by Eregon (Benoit Daloze) about 11 years ago


Please have a look at (or [ruby-core:35613]).

The reason is explained by Yusuke in [ruby-core:35682]:

2011/4/5 Benoit Daloze :

An unconditional nil is anyway not useful here, the only concern I see
might be the cost of creating this Array.

It is actually a problem.
I have used Array#zip with block for iteration many times. do |x, y|
p [x, y]

The change requires some people (including me) to rewrite
such a code as follows:

big_ary1.size.times do |i|
x, y = big_ary1[i], big_ary2[i]

So I like zip_with, if it is really needed.

That issue kind of got lost, it is probably a good idea to continue it.

For your example, we could have [1,2,3].zip_with([1,2,3], :+)

Updated by akr (Akira Tanaka) over 10 years ago

Currently it can be implemented as follows.

% ruby -e 'p [1,2,3].zip([1,2,3]).map {|a,b| a + b }'
[2, 4, 6]
% ruby -e 'p [1,2,3][1,2,3]).map {|a,b| a + b }.to_a'
[2, 4, 6]

Updated by mame (Yusuke Endoh) over 10 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

Updated by mame (Yusuke Endoh) almost 10 years ago

  • Target version set to 2.6
Actions #5

Updated by naruse (Yui NARUSE) almost 5 years ago

  • Target version deleted (2.6)

Updated by matz (Yukihiro Matsumoto) over 4 years ago

  • Status changed from Assigned to Rejected

We cannot change the behavior. The change would increase the memory consumption (and decrease the performance).


Actions #7

Updated by duerst (Martin Dürst) almost 3 years ago

  • Related to Feature #16261: Enumerable#each_splat and Enumerator#splat added

Also available in: Atom PDF