https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112018-03-07T23:32:57ZRuby Issue Tracking SystemRuby master - Feature #14585: Array#each_pairhttps://redmine.ruby-lang.org/issues/14585?journal_id=708522018-03-07T23:32:57Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>-1</p>
<p>If I saw a method <code>Array#each_pair</code> I'd expect it to behave like:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Array</span>
<span class="k">def</span> <span class="nf">each_pair</span>
<span class="n">each_with_index</span> <span class="k">do</span> <span class="o">|</span><span class="n">element</span><span class="p">,</span> <span class="n">index</span><span class="o">|</span>
<span class="k">yield</span> <span class="n">index</span><span class="p">,</span> <span class="n">element</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>Further, if you were to propose a method on Array that lets you iterate pair-wise I'd expect it to be like <code>each_slice</code>, not <code>each_cons</code>.</p> Ruby master - Feature #14585: Array#each_pairhttps://redmine.ruby-lang.org/issues/14585?journal_id=708532018-03-08T01:29:44Zsikachu (Prem Sichanugrist)s+ruby@sikac.hu
<ul></ul><blockquote>
<p>it may not be clear that Array#each_pair groups overlapping pairs vs. chunking elements</p>
</blockquote>
<p>Yeah, I think that's one reason I'm against an ambiguous method like this. I feel like <code>each_cons(2)</code> is already good for the job and more fitting for this. I also feel like the data structure of Array makes it so that each item actually stands on its own, unlike Hash where you know that there's always a key-value pair so grabbing "each pair" make sense.</p> Ruby master - Feature #14585: Array#each_pairhttps://redmine.ruby-lang.org/issues/14585?journal_id=708552018-03-08T04:33:59Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>I though same behavior as phluid61 wrote.</p> Ruby master - Feature #14585: Array#each_pairhttps://redmine.ruby-lang.org/issues/14585?journal_id=708572018-03-08T04:44:09Zsawa (Tsuyoshi Sawada)
<ul></ul><p>I think you have the wrong data structure in the first place. I think you either should have an array of arrays of length two, or a hash.</p> Ruby master - Feature #14585: Array#each_pairhttps://redmine.ruby-lang.org/issues/14585?journal_id=708992018-03-08T09:02:16ZHanmac (Hans Mackowiak)hanmac@gmx.de
<ul></ul><p>i don't like it because i think <code>each_pair</code> should react like <code>each_slice(2)</code></p> Ruby master - Feature #14585: Array#each_pairhttps://redmine.ruby-lang.org/issues/14585?journal_id=709392018-03-10T21:05:32Ziamvery (Jay Hayes)
<ul></ul><p>Thank you all for the feedback! I agree, and I'm glad your responses matched my intuition. Do you have any suggestions for how one might better remember <code>each_cons</code>? Ruby has a history of introducing aliases for the purpose of clarity. Can you think of such an alias for this method? For some reason my brain always defaults to "chunk", but perhaps that term is already too overloaded on Enumerable. Maybe it's just a matter of really committing it to memory, but I've certainly found it difficult to track down a few times now. Thanks again for taking the time to respond :)</p> Ruby master - Feature #14585: Array#each_pairhttps://redmine.ruby-lang.org/issues/14585?journal_id=709402018-03-10T22:16:08Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>iamvery (Jay Hayes) wrote:</p>
<blockquote>
<p>Thank you all for the feedback! I agree, and I'm glad your responses matched my intuition. Do you have any suggestions for how one might better remember <code>each_cons</code>? Ruby has a history of introducing aliases for the purpose of clarity. Can you think of such an alias for this method? For some reason my brain always defaults to "chunk", but perhaps that term is already too overloaded on Enumerable. Maybe it's just a matter of really committing it to memory, but I've certainly found it difficult to track down a few times now. Thanks again for taking the time to respond :)</p>
</blockquote>
<p>It's not really chunking, either, which means grouping elements into fewer larger chunks (like each_slice turning an array of 10 items into an array of 5 pairs)</p>
<p>What each_cons does is expose <strong>cons</strong>ecutive sub-sequences.</p> Ruby master - Feature #14585: Array#each_pairhttps://redmine.ruby-lang.org/issues/14585?journal_id=709502018-03-11T23:38:52Ziamvery (Jay Hayes)
<ul></ul><p>Great explanation, Matthew. Thank you! Sounds like I just need to practice this some more to really get the terminology into my vocabulary :)</p>