ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:44028] [ruby-trunk - Feature #6240][Open] Enumerable#drop with negative argument
@ 2012-03-31 20:57 marcandre (Marc-Andre Lafortune)
  2012-04-01  0:05 ` [ruby-core:44030] [ruby-trunk - Feature #6240] " shugo (Shugo Maeda)
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: marcandre (Marc-Andre Lafortune) @ 2012-03-31 20:57 UTC (permalink / raw)
  To: ruby-core


Issue #6240 has been reported by marcandre (Marc-Andre Lafortune).

----------------------------------------
Feature #6240: Enumerable#drop with negative argument
https://bugs.ruby-lang.org/issues/6240

Author: marcandre (Marc-Andre Lafortune)
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 2.0.0


Currently, Enumerable#drop works only for non-negative arguments.

It could be extended so that negative arguments means dropping from the end:

    [:hello, :world].drop(-1) # => [:hello]

This could especially be interesting for `Lazy#drop`, which would keep a circular buffer of elements before yielding them.

    (1..6).lazy.drop(-3).each{|x| puts x} # -> prints 1, 2 and 3

Thoughts?


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:44030] [ruby-trunk - Feature #6240] Enumerable#drop with negative argument
  2012-03-31 20:57 [ruby-core:44028] [ruby-trunk - Feature #6240][Open] Enumerable#drop with negative argument marcandre (Marc-Andre Lafortune)
@ 2012-04-01  0:05 ` shugo (Shugo Maeda)
  2012-04-01 23:38 ` [ruby-core:44047] " marcandre (Marc-Andre Lafortune)
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: shugo (Shugo Maeda) @ 2012-04-01  0:05 UTC (permalink / raw)
  To: ruby-core


Issue #6240 has been updated by shugo (Shugo Maeda).


marcandre (Marc-Andre Lafortune) wrote:
> Currently, Enumerable#drop works only for non-negative arguments.
> 
> It could be extended so that negative arguments means dropping from the end:
> 
>     [:hello, :world].drop(-1) # => [:hello]
> 
> This could especially be interesting for `Lazy#drop`, which would keep a circular buffer of elements before yielding them.
> 
>     (1..6).lazy.drop(-3).each{|x| puts x} # -> prints 1, 2 and 3
> 
> Thoughts?

How Enumerable#drop can know the total number of elements?
The source of elements might be IO.  Besides that, the total number of elements might be infinite.

----------------------------------------
Feature #6240: Enumerable#drop with negative argument
https://bugs.ruby-lang.org/issues/6240#change-25557

Author: marcandre (Marc-Andre Lafortune)
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 2.0.0


Currently, Enumerable#drop works only for non-negative arguments.

It could be extended so that negative arguments means dropping from the end:

    [:hello, :world].drop(-1) # => [:hello]

This could especially be interesting for `Lazy#drop`, which would keep a circular buffer of elements before yielding them.

    (1..6).lazy.drop(-3).each{|x| puts x} # -> prints 1, 2 and 3

Thoughts?


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:44047] [ruby-trunk - Feature #6240] Enumerable#drop with negative argument
  2012-03-31 20:57 [ruby-core:44028] [ruby-trunk - Feature #6240][Open] Enumerable#drop with negative argument marcandre (Marc-Andre Lafortune)
  2012-04-01  0:05 ` [ruby-core:44030] [ruby-trunk - Feature #6240] " shugo (Shugo Maeda)
@ 2012-04-01 23:38 ` marcandre (Marc-Andre Lafortune)
  2012-04-02  1:41 ` [ruby-core:44051] " shugo (Shugo Maeda)
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: marcandre (Marc-Andre Lafortune) @ 2012-04-01 23:38 UTC (permalink / raw)
  To: ruby-core


Issue #6240 has been updated by marcandre (Marc-Andre Lafortune).


Hi,

shugo (Shugo Maeda) wrote:
> How Enumerable#drop can know the total number of elements?
> The source of elements might be IO.  Besides that, the total number of elements might be infinite.

`drop` 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:

class Lazy
  def drop(n)
    return to_enum :drop, n unless block_given?
    if n < 0
      buffer = []
      each do |e|
        buffer << e
        yield buffer.shift if buffer.size > -n
      end
    else
      # ...
    end
  end
end

For infinite sequences, `drop` with negative argument would not be very useful, but it would still yield all elements.
----------------------------------------
Feature #6240: Enumerable#drop with negative argument
https://bugs.ruby-lang.org/issues/6240#change-25576

Author: marcandre (Marc-Andre Lafortune)
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 2.0.0


Currently, Enumerable#drop works only for non-negative arguments.

It could be extended so that negative arguments means dropping from the end:

    [:hello, :world].drop(-1) # => [:hello]

This could especially be interesting for `Lazy#drop`, which would keep a circular buffer of elements before yielding them.

    (1..6).lazy.drop(-3).each{|x| puts x} # -> prints 1, 2 and 3

Thoughts?


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:44051] [ruby-trunk - Feature #6240] Enumerable#drop with negative argument
  2012-03-31 20:57 [ruby-core:44028] [ruby-trunk - Feature #6240][Open] Enumerable#drop with negative argument marcandre (Marc-Andre Lafortune)
  2012-04-01  0:05 ` [ruby-core:44030] [ruby-trunk - Feature #6240] " shugo (Shugo Maeda)
  2012-04-01 23:38 ` [ruby-core:44047] " marcandre (Marc-Andre Lafortune)
@ 2012-04-02  1:41 ` shugo (Shugo Maeda)
  2012-04-02 13:59 ` [ruby-core:44069] [ruby-trunk - Feature #6240][Assigned] " mame (Yusuke Endoh)
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: shugo (Shugo Maeda) @ 2012-04-02  1:41 UTC (permalink / raw)
  To: ruby-core


Issue #6240 has been updated by shugo (Shugo Maeda).


marcandre (Marc-Andre Lafortune) wrote:
> > How Enumerable#drop can know the total number of elements?
> > The source of elements might be IO.  Besides that, the total number of elements might be infinite.
> 
> `drop` 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:

Ah, I see.  It's interesting.

Do you have any use case in mind?

----------------------------------------
Feature #6240: Enumerable#drop with negative argument
https://bugs.ruby-lang.org/issues/6240#change-25581

Author: marcandre (Marc-Andre Lafortune)
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 2.0.0


Currently, Enumerable#drop works only for non-negative arguments.

It could be extended so that negative arguments means dropping from the end:

    [:hello, :world].drop(-1) # => [:hello]

This could especially be interesting for `Lazy#drop`, which would keep a circular buffer of elements before yielding them.

    (1..6).lazy.drop(-3).each{|x| puts x} # -> prints 1, 2 and 3

Thoughts?


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:44069] [ruby-trunk - Feature #6240][Assigned] Enumerable#drop with negative argument
  2012-03-31 20:57 [ruby-core:44028] [ruby-trunk - Feature #6240][Open] Enumerable#drop with negative argument marcandre (Marc-Andre Lafortune)
                   ` (2 preceding siblings ...)
  2012-04-02  1:41 ` [ruby-core:44051] " shugo (Shugo Maeda)
@ 2012-04-02 13:59 ` mame (Yusuke Endoh)
  2012-10-25  8:48 ` [ruby-core:48242] [ruby-trunk - Feature #6240] " yhara (Yutaka HARA)
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mame (Yusuke Endoh) @ 2012-04-02 13:59 UTC (permalink / raw)
  To: ruby-core


Issue #6240 has been updated by mame (Yusuke Endoh).

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


----------------------------------------
Feature #6240: Enumerable#drop with negative argument
https://bugs.ruby-lang.org/issues/6240#change-25602

Author: marcandre (Marc-Andre Lafortune)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Currently, Enumerable#drop works only for non-negative arguments.

It could be extended so that negative arguments means dropping from the end:

    [:hello, :world].drop(-1) # => [:hello]

This could especially be interesting for `Lazy#drop`, which would keep a circular buffer of elements before yielding them.

    (1..6).lazy.drop(-3).each{|x| puts x} # -> prints 1, 2 and 3

Thoughts?


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:48242] [ruby-trunk - Feature #6240] Enumerable#drop with negative argument
  2012-03-31 20:57 [ruby-core:44028] [ruby-trunk - Feature #6240][Open] Enumerable#drop with negative argument marcandre (Marc-Andre Lafortune)
                   ` (3 preceding siblings ...)
  2012-04-02 13:59 ` [ruby-core:44069] [ruby-trunk - Feature #6240][Assigned] " mame (Yusuke Endoh)
@ 2012-10-25  8:48 ` yhara (Yutaka HARA)
  2019-01-10  8:30 ` [ruby-core:90982] [Ruby trunk Feature#6240] " knu
  2019-01-10 23:07 ` [ruby-core:91006] [Ruby trunk Feature#6240][Closed] " ruby-core
  6 siblings, 0 replies; 8+ messages in thread
From: yhara (Yutaka HARA) @ 2012-10-25  8:48 UTC (permalink / raw)
  To: ruby-core


Issue #6240 has been updated by yhara (Yutaka HARA).

Target version changed from 2.0.0 to next minor


----------------------------------------
Feature #6240: Enumerable#drop with negative argument
https://bugs.ruby-lang.org/issues/6240#change-31517

Author: marcandre (Marc-Andre Lafortune)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


Currently, Enumerable#drop works only for non-negative arguments.

It could be extended so that negative arguments means dropping from the end:

    [:hello, :world].drop(-1) # => [:hello]

This could especially be interesting for `Lazy#drop`, which would keep a circular buffer of elements before yielding them.

    (1..6).lazy.drop(-3).each{|x| puts x} # -> prints 1, 2 and 3

Thoughts?


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:90982] [Ruby trunk Feature#6240] Enumerable#drop with negative argument
  2012-03-31 20:57 [ruby-core:44028] [ruby-trunk - Feature #6240][Open] Enumerable#drop with negative argument marcandre (Marc-Andre Lafortune)
                   ` (4 preceding siblings ...)
  2012-10-25  8:48 ` [ruby-core:48242] [ruby-trunk - Feature #6240] " yhara (Yutaka HARA)
@ 2019-01-10  8:30 ` knu
  2019-01-10 23:07 ` [ruby-core:91006] [Ruby trunk Feature#6240][Closed] " ruby-core
  6 siblings, 0 replies; 8+ messages in thread
From: knu @ 2019-01-10  8:30 UTC (permalink / raw)
  To: ruby-core

Issue #6240 has been updated by knu (Akinori MUSHA).


Using an existing feature, `[1,2,3,4,5].lazy.drop(-n)` could be written as `[1,2,3,4,5].each_cons(n+1).lazy.map(&:first)`.
Enumerable#each_cons does not use a circular buffer, though. (It currently uses push & shift)

----------------------------------------
Feature #6240: Enumerable#drop with negative argument
https://bugs.ruby-lang.org/issues/6240#change-76199

* Author: marcandre (Marc-Andre Lafortune)
* Status: Feedback
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
Currently, Enumerable#drop works only for non-negative arguments.

It could be extended so that negative arguments means dropping from the end:

    [:hello, :world].drop(-1) # => [:hello]

This could especially be interesting for `Lazy#drop`, which would keep a circular buffer of elements before yielding them.

    (1..6).lazy.drop(-3).each{|x| puts x} # -> prints 1, 2 and 3

Thoughts?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:91006] [Ruby trunk Feature#6240][Closed] Enumerable#drop with negative argument
  2012-03-31 20:57 [ruby-core:44028] [ruby-trunk - Feature #6240][Open] Enumerable#drop with negative argument marcandre (Marc-Andre Lafortune)
                   ` (5 preceding siblings ...)
  2019-01-10  8:30 ` [ruby-core:90982] [Ruby trunk Feature#6240] " knu
@ 2019-01-10 23:07 ` ruby-core
  6 siblings, 0 replies; 8+ messages in thread
From: ruby-core @ 2019-01-10 23:07 UTC (permalink / raw)
  To: ruby-core

Issue #6240 has been updated by marcandre (Marc-Andre Lafortune).

Status changed from Feedback to Closed

knu (Akinori MUSHA) wrote:
> Using an existing feature, `[1,2,3,4,5].lazy.drop(-n)` could be written as `[1,2,3,4,5].each_cons(n+1).lazy.map(&:first)`.
> Enumerable#each_cons does not use a circular buffer, though. (It currently uses push & shift)

That is clever.
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).

----------------------------------------
Feature #6240: Enumerable#drop with negative argument
https://bugs.ruby-lang.org/issues/6240#change-76228

* Author: marcandre (Marc-Andre Lafortune)
* Status: Closed
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
Currently, Enumerable#drop works only for non-negative arguments.

It could be extended so that negative arguments means dropping from the end:

    [:hello, :world].drop(-1) # => [:hello]

This could especially be interesting for `Lazy#drop`, which would keep a circular buffer of elements before yielding them.

    (1..6).lazy.drop(-3).each{|x| puts x} # -> prints 1, 2 and 3

Thoughts?



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-01-10 23:07 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-31 20:57 [ruby-core:44028] [ruby-trunk - Feature #6240][Open] Enumerable#drop with negative argument marcandre (Marc-Andre Lafortune)
2012-04-01  0:05 ` [ruby-core:44030] [ruby-trunk - Feature #6240] " shugo (Shugo Maeda)
2012-04-01 23:38 ` [ruby-core:44047] " marcandre (Marc-Andre Lafortune)
2012-04-02  1:41 ` [ruby-core:44051] " shugo (Shugo Maeda)
2012-04-02 13:59 ` [ruby-core:44069] [ruby-trunk - Feature #6240][Assigned] " mame (Yusuke Endoh)
2012-10-25  8:48 ` [ruby-core:48242] [ruby-trunk - Feature #6240] " yhara (Yutaka HARA)
2019-01-10  8:30 ` [ruby-core:90982] [Ruby trunk Feature#6240] " knu
2019-01-10 23:07 ` [ruby-core:91006] [Ruby trunk Feature#6240][Closed] " ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).