https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112012-04-01T09:05:51ZRuby Issue Tracking SystemRuby master - Feature #6240: Enumerable#drop with negative argumenthttps://bugs.ruby-lang.org/issues/6240?journal_id=255572012-04-01T09:05:51Zshugo (Shugo Maeda)
<ul></ul><p>marcandre (Marc-Andre Lafortune) wrote:</p>
<blockquote>
<p>Currently, Enumerable#drop works only for non-negative arguments.</p>
<p>It could be extended so that negative arguments means dropping from the end:</p>
<pre><code>[:hello, :world].drop(-1) # => [:hello]
</code></pre>
<p>This could especially be interesting for <code>Lazy#drop</code>, which would keep a circular buffer of elements before yielding them.</p>
<pre><code>(1..6).lazy.drop(-3).each{|x| puts x} # -> prints 1, 2 and 3
</code></pre>
<p>Thoughts?</p>
</blockquote>
<p>How Enumerable#drop can know the total number of elements?<br>
The source of elements might be IO. Besides that, the total number of elements might be infinite.</p> Ruby master - Feature #6240: Enumerable#drop with negative argumenthttps://bugs.ruby-lang.org/issues/6240?journal_id=255762012-04-02T08:38:30Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Hi,</p>
<p>shugo (Shugo Maeda) wrote:</p>
<blockquote>
<p>How Enumerable#drop can know the total number of elements?<br>
The source of elements might be IO. Besides that, the total number of elements might be infinite.</p>
</blockquote>
<p><code>drop</code> would have to consume the whole iteration, indeed, which is why I was talking about a buffer. The buffer holds elements until we know they are not in the last (-n) elements. Here's a Ruby implementation:</p>
<p>class Lazy<br>
def drop(n)<br>
return to_enum :drop, n unless block_given?<br>
if n < 0<br>
buffer = []<br>
each do |e|<br>
buffer << e<br>
yield buffer.shift if buffer.size > -n<br>
end<br>
else<br>
# ...<br>
end<br>
end<br>
end</p>
<p>For infinite sequences, <code>drop</code> with negative argument would not be very useful, but it would still yield all elements.</p> Ruby master - Feature #6240: Enumerable#drop with negative argumenthttps://bugs.ruby-lang.org/issues/6240?journal_id=255812012-04-02T10:41:53Zshugo (Shugo Maeda)
<ul></ul><p>marcandre (Marc-Andre Lafortune) wrote:</p>
<blockquote>
<blockquote>
<p>How Enumerable#drop can know the total number of elements?<br>
The source of elements might be IO. Besides that, the total number of elements might be infinite.</p>
</blockquote>
<p><code>drop</code> would have to consume the whole iteration, indeed, which is why I was talking about a buffer. The buffer holds elements until we know they are not in the last (-n) elements. Here's a Ruby implementation:</p>
</blockquote>
<p>Ah, I see. It's interesting.</p>
<p>Do you have any use case in mind?</p> Ruby master - Feature #6240: Enumerable#drop with negative argumenthttps://bugs.ruby-lang.org/issues/6240?journal_id=256022012-04-02T22:59:45Zmame (Yusuke Endoh)mame@ruby-lang.org
<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 #6240: Enumerable#drop with negative argumenthttps://bugs.ruby-lang.org/issues/6240?journal_id=315172012-10-25T17:48:26Zyhara (Yutaka HARA)
<ul><li><strong>Target version</strong> changed from <i>2.0.0</i> to <i>2.6</i></li></ul> Ruby master - Feature #6240: Enumerable#drop with negative argumenthttps://bugs.ruby-lang.org/issues/6240?journal_id=687592017-12-25T18:15:04Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Target version</strong> deleted (<del><i>2.6</i></del>)</li></ul> Ruby master - Feature #6240: Enumerable#drop with negative argumenthttps://bugs.ruby-lang.org/issues/6240?journal_id=761962019-01-10T08:23:44Zusa (Usaku NAKAMURA)usa@garbagecollect.jp
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Feedback</i></li></ul> Ruby master - Feature #6240: Enumerable#drop with negative argumenthttps://bugs.ruby-lang.org/issues/6240?journal_id=761992019-01-10T08:30:44Zknu (Akinori MUSHA)knu@ruby-lang.org
<ul></ul><p>Using an existing feature, <code>[1,2,3,4,5].lazy.drop(-n)</code> could be written as <code>[1,2,3,4,5].each_cons(n+1).lazy.map(&:first)</code>.<br>
Enumerable#each_cons does not use a circular buffer, though. (It currently uses push & shift)</p> Ruby master - Feature #6240: Enumerable#drop with negative argumenthttps://bugs.ruby-lang.org/issues/6240?journal_id=762282019-01-10T23:07:22Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Feedback</i> to <i>Closed</i></li></ul><p>knu (Akinori MUSHA) wrote:</p>
<blockquote>
<p>Using an existing feature, <code>[1,2,3,4,5].lazy.drop(-n)</code> could be written as <code>[1,2,3,4,5].each_cons(n+1).lazy.map(&:first)</code>.<br>
Enumerable#each_cons does not use a circular buffer, though. (It currently uses push & shift)</p>
</blockquote>
<p>That is clever.<br>
I'll close this request, since it's not clear how frequent this could be needed and there's already an easy way of doing it (even though it might not be obvious).</p>