Feature #5044

#zip with block return mapped results

Added by Thomas Sawyer almost 3 years ago. Updated over 1 year ago.

[ruby-core:38154]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:-
Target version:next minor

Description

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

Related to ruby-trunk - Feature #4539: Array#zip_with Assigned 03/30/2011

History

#1 Updated by Benoit Daloze almost 3 years ago

Hi,

Please have a look at http://redmine.ruby-lang.org/issues/4539 (or ).

The reason is explained by Yusuke in :

2011/4/5 Benoit Daloze eregontp@gmail.com:

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.

bigary1.zip(bigary2) do |x, y|
p [x, y]
end

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

bigary1.size.times do |i|
x, y = big
ary1[i], big_ary2[i]
...
end

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], :+)

#2 Updated by Akira Tanaka about 2 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].lazy.zip([1,2,3]).map {|a,b| a + b }.to_a'
[2, 4, 6]

#3 Updated by Yusuke Endoh about 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto

#4 Updated by Yusuke Endoh over 1 year ago

  • Target version set to next minor

Also available in: Atom PDF