Bug #7877

E::Lazy#with_index should be lazy

Added by Shyouhei Urabe about 1 year ago. Updated 3 months ago.

[ruby-dev:47025]
Status:Assigned
Priority:Normal
Assignee:Yutaka HARA
Category:core
Target version:current: 2.2.0
ruby -v:2.1.0-dev Backport:2.0.0: UNKNOWN

Description

=begin
So I wanted some real benefit of being lazy. I wrote a Leibniz formula:

def leibniz(n)
(0..Float::INFINITY).lazy.withindex {|i, j| (-1 ** j) / (2*i+1).tof }.take(n).reduce(:+)
end

But it doesn't work (well, it does, indeed. It just doesn't stop working). I got frustrated.
How about it? Don't you feel it nifty?

Of course I can wait for the release next to 2.0.0.
=end

0001-enumerator.c-Enumerator-Lazy-with_index.patch Magnifier (3.33 KB) Nobuyoshi Nakada, 02/19/2013 03:25 PM

History

#2 Updated by Marc-Andre Lafortune about 1 year ago

See #7696 on how to handle state...

#3 Updated by Shyouhei Urabe about 1 year ago

  • Description updated (diff)

OK, so @marcandre is interested in. I re-wrote the description in English.

#4 Updated by Marc-Andre Lafortune about 1 year ago

Note that (thanks to #7715), you can use with_index without a block and follow it with map:

def leibniz(n)
  (0..Float::INFINITY).lazy.with_index.map {|i, j| (-1 ** j) / (2*i+1).to_f }.take(n).reduce(:+)
end

I'm neutral about this feature request. The problem I see is that it's too late for 2.0.0 and it would introduce potential incompatibility in next minor.

#5 Updated by Shyouhei Urabe about 1 year ago

@marcandre oh, thank you! You saved my day.

Still I want this though.

#6 Updated by Zachary Scott about 1 year ago

Propose to move this to next major?

#7 Updated by Martin Dürst about 1 year ago

zzak (Zachary Scott) wrote:

Propose to move this to next major?

Do you mean because of "potential incompatibility" (https://bugs.ruby-lang.org/issues/7877#note-4)? What exactly would this incompatibility be? To me, it seems that lazy.with_index would just work, so we should just fix it. Next major seems way too long to wait.

#8 Updated by Zachary Scott 11 months ago

@duerst You're probably right, since this feature was introduced in 2.0.0

If yhara-san wants to implement #with_index with a block then I see no problem with introducing this in 2.1.0

#9 Updated by Zachary Scott 11 months ago

  • Tracker changed from Feature to Bug
  • Subject changed from E::Lazy#with_index needed to E::Lazy#with_index should be lazy
  • Target version changed from next minor to 2.1.0
  • ruby -v set to 2.1.0-dev
  • Backport set to 2.0.0: UNKNOWN

#10 Updated by Hiroshi SHIBATA 3 months ago

  • Target version changed from 2.1.0 to current: 2.2.0

Also available in: Atom PDF