https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112011-04-05T15:55:12ZRuby Issue Tracking SystemRuby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=162822011-04-05T15:55:12Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=162862011-04-05T20:23:06ZEregon (Benoit Daloze)
<ul></ul><blockquote>
<p><a href="http://redmine.ruby-lang.org/issues/4539" class="external">http://redmine.ruby-lang.org/issues/4539</a></p>
<p>Author: Michael Kohl</p>
<p>Inspired by Haskell's <code>zipWith</code> function, I hacked on together for Ruby:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">].</span><span class="nf">zip_with</span><span class="p">([</span><span class="mi">6</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span> <span class="p">:</span><span class="o">+</span><span class="p">)</span> <span class="c1">#=> [7, 7, 7]</span>
<span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">].</span><span class="nf">zip_with</span><span class="p">([</span><span class="mi">6</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">4</span><span class="p">])</span> <span class="p">{</span> <span class="o">|</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="o">|</span> <span class="mi">3</span><span class="o">*</span><span class="n">a</span><span class="o">+</span><span class="mi">2</span><span class="o">*</span><span class="n">b</span> <span class="p">}</span> <span class="c1">#=> [15, 16, 17]</span>
</code></pre>
<p>So far I only have a Ruby version of it:</p>
<p><a href="https://gist.github.com/731702b90757e21cadcb" class="external">https://gist.github.com/731702b90757e21cadcb</a></p>
<p>My questions:</p>
<ol>
<li>
<p>Would this method be considered a worthwhile addition to <code>Array</code>?</p>
</li>
<li>
<p>I've never hacked on the C side of Ruby (read some parts of the source though) and my C is quite rusty. I'd like to change that, would somebody be willing to help me turn this into a proper patch?</p>
</li>
</ol>
</blockquote>
<p>Hello,</p>
<p>I'm answering here since redmine won't answer to my requests (even got a 500).</p>
<p>I think this new method would be redundant with <code>Array#zip</code>.<br>
But I agree doing <code>c.zip(d).map { |a,b| a+b }</code> is a bit long.</p>
<p>So I propose to change the return value of Array#zip (and<br>
Enumerable#zip) with a block from nil to the result #map would give<br>
(so an Array of all yielded elements).</p>
<p>An unconditional nil is anyway not useful here, the only concern I see<br>
might be the cost of creating this Array.<br>
But I think many cases with block simulate #map and it would avoid<br>
creating the intermediate Array in a.zip(b).map {}.</p>
<p>This would not address the case with a Symbol, which could be easily<br>
detected as last argument as it is not Enumerable.<br>
But blocks simplified by Symbol have rarely been accepted and I think<br>
only changing the return value would already improve #zip a lot.</p>
<p>Here is a gist with a Ruby implementation of the modifications for<br>
Array#zip: <a href="https://gist.github.com/903388" class="external">https://gist.github.com/903388</a><br>
I'm wishing to do a C implementation if this feels right for others,<br>
but I'd like to have opinions first.</p>
<p>What do you think?</p> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=163372011-04-09T18:23:06Zmrkn (Kenta Murata)muraken@gmail.com
<ul></ul><p>=begin<br>
Hi,</p>
<p>On 2011年4月5日火曜日 at 19:50, Benoit Daloze wrote:</p>
<blockquote>
<p>Here is a gist with a Ruby implementation of the modifications for<br>
Array#zip: <a href="https://gist.github.com/903388" class="external">https://gist.github.com/903388</a><br>
I'm wishing to do a C implementation if this feels right for others,<br>
but I'd like to have opinions first.<br>
I implemented the features in C, and wrote tests for them.<br>
Please see the following diffs:<br>
<a href="https://github.com/mrkn/ruby/commit/9c7ead0e385b6a17dafa5bc8b4389e1baf2e3040" class="external">https://github.com/mrkn/ruby/commit/9c7ead0e385b6a17dafa5bc8b4389e1baf2e3040</a></p>
</blockquote>
<p>I will commit this if matz will approve.</p>
<p>--<br>
Kenta Murata<br>
Sent with Sparrow<br>
=end</p> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=163382011-04-09T22:23:06Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>=begin<br>
Hi,</p>
<p>In message "Re: <a href="/issues/4539">[ruby-core:35673]</a> Re: [Ruby 1.9 - Feature <a class="issue tracker-2 status-2 priority-4 priority-default" title="Feature: Array#zip_with (Assigned)" href="https://bugs.ruby-lang.org/issues/4539">#4539</a>][Assigned] Array#zip_with"<br>
on Sat, 9 Apr 2011 17:29:28 +0900, Kenta Murata <a href="mailto:muraken@gmail.com" class="email">muraken@gmail.com</a> writes:</p>
<p>|I implemented the features in C, and wrote tests for them.<br>
|Please see the following diffs:<br>
|<a href="https://github.com/mrkn/ruby/commit/9c7ead0e385b6a17dafa5bc8b4389e1baf2e3040" class="external">https://github.com/mrkn/ruby/commit/9c7ead0e385b6a17dafa5bc8b4389e1baf2e3040</a><br>
|<br>
|I will commit this if matz will approve.</p>
<p>I am not sure whether adding new zip_with or adding zip with symbol at<br>
last would be better. Any opinion?</p>
<pre><code> matz.
</code></pre>
<p>=end</p> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=163392011-04-09T23:23:06ZEregon (Benoit Daloze)
<ul></ul><p>=begin<br>
Hi,<br>
On 9 April 2011 10:29, Kenta Murata <a href="mailto:muraken@gmail.com" class="email">muraken@gmail.com</a> wrote:</p>
<blockquote>
<p>Hi,</p>
<p>I implemented the features in C, and wrote tests for them.<br>
Please see the following diffs:<br>
<a href="https://github.com/mrkn/ruby/commit/9c7ead0e385b6a17dafa5bc8b4389e1baf2e3040" class="external">https://github.com/mrkn/ruby/commit/9c7ead0e385b6a17dafa5bc8b4389e1baf2e3040</a></p>
</blockquote>
<p>Thank you, nice diff.</p>
<p>It would be nice to add the two examples from Michael Kohl:<br>
[1,2,3].zip([6,5,4], :+) #=> [7, 7, 7]<br>
[1,2,3].zip([6,5,4]) { |a,b| 3<em>a+2</em>b } #=> [15, 16, 17]</p>
<p>And of course do the same for Enumerable#zip.</p>
<p>Just a quick notice: in the tests, the arguments are reversed (it is<br>
assert_equal(expected, actual, msg = nil)).<br>
It is always hard to remember with test/unit, that is a reason why I<br>
prefer the spec syntax in general.<br>
It is definitely just a detail, but it could mislead someone reading a<br>
failing test.<br>
=end</p> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=163402011-04-10T00:23:06ZEregon (Benoit Daloze)
<ul></ul><p>=begin<br>
Hello,<br>
On 9 April 2011 15:13, Yukihiro Matsumoto <a href="mailto:matz@ruby-lang.org" class="email">matz@ruby-lang.org</a> wrote:<br>
| Hi,<br>
|<br>
| I am not sure whether adding new zip_with or adding zip with symbol at<br>
| last would be better. Â Any opinion?<br>
|<br>
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â matz.</p>
<p>Although I already presented my opinion, I'd like to clarify it.</p>
<p>I think adding a new method for just one special form is not worth it.<br>
Also, a method name ending with a preposition is rather unusual in core/stdlib.</p>
<p>I feel honored to see you here,<br>
B.D.<br>
=end</p> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=163412011-04-10T12:23:05Zsorah (Sorah Fukumori)her@sorah.jp
<ul></ul><p>=begin<br>
HI,</p>
<p>On Sat, Apr 9, 2011 at 10:13 PM, Yukihiro Matsumoto <a href="mailto:matz@ruby-lang.org" class="email">matz@ruby-lang.org</a> wrote:</p>
<blockquote>
<p>I am not sure whether adding new zip_with or adding zip with symbol at<br>
last would be better. Â Any opinion?</p>
</blockquote>
<p>I vote to adding zip with symbol at last.</p>
<p>Because zip_with method name is so long and hard to understand behavior.</p>
<p>--<br>
Shota Fukumori a.k.a. @sora_h - <a href="http://codnote.net/" class="external">http://codnote.net/</a><br>
=end</p> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=163422011-04-10T12:29:06Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>=begin<br>
Hi,</p>
<p>2011/4/9 Yukihiro Matsumoto <a href="mailto:matz@ruby-lang.org" class="email">matz@ruby-lang.org</a>:</p>
<blockquote>
<p>I am not sure whether adding new zip_with or adding zip with symbol at<br>
last would be better. Â Any opinion?</p>
</blockquote>
<p>I'm neutral for adding zip with symbol at last, but I<br>
object to letting zip with block return a new array.</p>
<p>2011/4/5 Benoit Daloze <a href="mailto:eregontp@gmail.com" class="email">eregontp@gmail.com</a>:</p>
<blockquote>
<p>An unconditional nil is anyway not useful here, the only concern I see<br>
might be the cost of creating this Array.</p>
</blockquote>
<p>It is actually a problem.<br>
I have used Array#zip with block for iteration many times.</p>
<p>big_ary1.zip(big_ary2) do |x, y|<br>
p [x, y]<br>
end</p>
<p>The change requires some people (including me) to <em>rewrite</em><br>
such a code as follows:</p>
<p>big_ary1.size.times do |i|<br>
x, y<br>
=end</p> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=163442011-04-10T22:23:10Zaprescott (Adam Prescott)
<ul></ul><p>=begin<br>
On Sat, Apr 9, 2011 at 2:13 PM, Yukihiro Matsumoto <a href="mailto:matz@ruby-lang.org" class="email">matz@ruby-lang.org</a> wrote:</p>
<blockquote>
<p>I am not sure whether adding new zip_with or adding zip with symbol at<br>
last would be better. Any opinion?</p>
<p> matz.</p>
</blockquote>
<p>An issue I have with zip taking a symbol is that ary1.zip(ary2, :+)<br>
doesn't actually return a zipped array (of arrays). In that respect,<br>
the result and the name "zip" don't align, with the additional<br>
argument.</p>
<p>Is there such a need for this in core that we need a shortcut around<br>
ary1.zip(ary2).map { |a, b| a + b }?<br>
=end</p> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=163472011-04-11T07:23:06Zmrkn (Kenta Murata)muraken@gmail.com
<ul></ul><p>=begin<br>
Hi,</p>
<p>On 2011年4月10日日曜日 at 11:56, Marc-Andre Lafortune wrote:</p>
<blockquote>
<p>On Sat, Apr 9, 2011 at 4:29 AM, Kenta Murata <a href="mailto:muraken@gmail.com" class="email">muraken@gmail.com</a> wrote:</p>
<blockquote>
<p>I implemented the features in C, and wrote tests for them.<br>
Please see the following diffs:<br>
<a href="https://github.com/mrkn/ruby/commit/9c7ead0e385b6a17dafa5bc8b4389e1baf2e3040" class="external">https://github.com/mrkn/ruby/commit/9c7ead0e385b6a17dafa5bc8b4389e1baf2e3040</a></p>
</blockquote>
<p>Looks good, but is there a reason to use <code>rb_funcall</code> for the reduce?<br>
Typically MRI calls directly the C implementation wherever possible,<br>
and I would suggest doing that here too.<br>
Tee patch was updated:<br>
<a href="https://github.com/mrkn/ruby/commit/fef79bb11e7d1173e31ae7a6e5472ac10eac9316" class="external">https://github.com/mrkn/ruby/commit/fef79bb11e7d1173e31ae7a6e5472ac10eac9316</a></p>
</blockquote>
<p>This changes add a new public function of libruby, rb_enum_inject_with_symbol.<br>
So we should discuss whether the function is acceptible as a public function.</p>
<p>--<br>
Kenta Murata<br>
Sent with Sparrow<br>
=end</p> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=165992011-04-24T13:01:11Zjvoorhis (Jeremy Voorhis)jvoorhis@gmail.com
<ul></ul><p>=begin<br>
I think it's worth having Enumerable#zip_with as a new public method. zip_with is the generalization of zip (you can define zip = zipWIth (,) in Haskell). Because zip_with can be implemented directly via inject, it's possible to provide an implementation with a lower complexity than zip composed with map. With respect to the comment about a method ending in a preposition, the Haskell standard provides a good precedent, and a worthy addition to the collection of languages that influenced Ruby.<br>
=end</p> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=192972011-07-18T23:58:53Ztrans (Thomas Sawyer)
<ul></ul><p>@Endoh Why would you have to rewrite? You can still iterate, just don't use the return result.</p> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=192982011-07-19T00:00:39Ztrans (Thomas Sawyer)
<ul></ul><p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/13">@matz (Yukihiro Matsumoto)</a> is symbol really needed?</p>
<pre><code>class Array
def zip(a, &b1)
if b
r = []
b2 = lambda{ |x| r << b1.call(*x) }
super(a, &b2)
r
else
super(a)
end
end
end
[1,2,3].zip([5,6,7], &:+) #=> [6,8,10]
</code></pre> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=192992011-07-19T00:06:52Ztrans (Thomas Sawyer)
<ul></ul><p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/9699">@adam (Adam M)</a> Perhaps you are right. Perhaps the real issue is why #map can't take optional "zipping" arguments?</p> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=193702011-07-20T12:59:13Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>Hello,</p>
<p>2011/7/18 Thomas Sawyer <a href="mailto:transfire@gmail.com" class="email">transfire@gmail.com</a>:</p>
<blockquote>
<p>@Endoh  Why would you have to rewrite? You can still iterate, just don't use the return result.</p>
</blockquote>
<p>I have used Array#zip in hot-path code to avoid unused array<br>
generation. So I don't want it to generate a unused and big<br>
array. But this is just my personal opinion, not a decision.<br>
If matz says ok, it is ok.</p>
<p>BTW, ko1 is now studying optimization to avoid unnecessary<br>
object generation by using escape analysis.<br>
If it is achieved, my concern will be pointless.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a></p> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=194692011-07-21T20:53:10Zaprescott (Adam Prescott)
<ul></ul><p>On Mon, Jul 18, 2011 at 4:06 PM, Thomas Sawyer <a href="mailto:transfire@gmail.com" class="email">transfire@gmail.com</a> wrote:</p>
<blockquote>
<p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/9699">@adam (Adam M)</a> Perhaps you are right. Perhaps the real issue is why #map can't take<br>
optional "zipping" arguments?</p>
</blockquote>
<p>I believe there is a suggestion from ruby-talk to call it map_with, instead.<br>
I agree with leaning towards #map instead of #zip, whatever happens.</p> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=318092012-10-27T23:53:27Zyhara (Yutaka HARA)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/31809/diff?detail_id=22407">diff</a>)</li><li><strong>Target version</strong> set to <i>2.6</i></li></ul> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=686792017-12-25T18:14:59Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Target version</strong> deleted (<del><i>2.6</i></del>)</li></ul> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=704862018-02-20T08:54:47Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/70486/diff?detail_id=48353">diff</a>)</li></ul> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=822512019-10-22T22:48:29Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-6 priority-4 priority-default closed" href="/issues/16261">Feature #16261</a>: Enumerable#each_splat and Enumerator#splat</i> added</li></ul> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=822882019-10-23T19:37:52Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>Martin added this to the next developer meeting. I have not yet commented on<br>
this issue so I may briefly do so.</p>
<p>matz asked back then between zip_with, or zip with symbol. I think zip_with may<br>
be better than zip with symbol from a use-point of view.</p>
<p>As for zip_with versus map_with as shown by aprescott - I think zip_with may be<br>
better than putting an additional map_* name. I guess you could reason for an<br>
alias either way, but perhaps it would be better to keep it simple and start<br>
only with zip_with, see whether this may be used at all, before considering<br>
map_* changes (as a name; keep in mind that we have other use cases already<br>
with .map, such as .map.with_index(2) and such. This is also another reason<br>
why I think zip_* would be better than a map_* change here. But you could<br>
also reason either way if people don't read docs, and want to use a .map_*<br>
variant instead. :P Either way, I think it would be better to see for<br>
the potential use cases for .zip_with first).</p>
<p>It may also be worthwhile to ask mame for his opinion again too, years later,<br>
to see how/if any opinions changed/adapted/stayed the same or not in regards<br>
to the feature. :) (Actually, if it is a new method, then any object allocation<br>
situation may not be very important, since it would not effect more general<br>
use cases, e. g. current use of .zip() and such; but I do not know the C<br>
internals so I have no real clue).</p> Ruby master - Feature #4539: Array#zip_withhttps://bugs.ruby-lang.org/issues/4539?journal_id=828572019-11-28T08:23:40Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><ul>
<li>The name <code>zip_with</code> is too confusing with <code>zip</code>. We need a new name.</li>
<li>The behavior can be described by the combination of <code>zip</code> and <code>map</code>.</li>
<li>I am not sure how much we need this behavior (yet).</li>
</ul>
<p>Matz.</p>