Bug #7877

E::Lazy#with_index should be lazy

Added by Shyouhei Urabe about 4 years ago. Updated 4 months ago.

Target version:
ruby -v:


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

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

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.

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


#2 [ruby-dev:47046] Updated by Marc-Andre Lafortune about 4 years ago

See #7696 on how to handle state...

#3 [ruby-dev:47047] Updated by Shyouhei Urabe almost 4 years ago

  • Description updated (diff)

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

#4 [ruby-dev:47050] Updated by Marc-Andre Lafortune almost 4 years 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) {|i, j| (-1 ** j) / (2*i+1).to_f }.take(n).reduce(:+)

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 [ruby-dev:47056] Updated by Shyouhei Urabe almost 4 years ago

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

Still I want this though.

#6 [ruby-dev:47225] Updated by Zachary Scott almost 4 years ago

Propose to move this to next major?

#7 [ruby-dev:47226] Updated by Martin Dürst almost 4 years ago

zzak (Zachary Scott) wrote:

Propose to move this to next major?

Do you mean because of "potential incompatibility" ( 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 [ruby-dev:47390] Updated by Zachary Scott over 3 years 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 [ruby-dev:47392] Updated by Zachary Scott over 3 years ago

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

#10 [ruby-dev:47932] Updated by Hiroshi SHIBATA about 3 years ago

  • Target version changed from 2.1.0 to 2.2.0

#11 [ruby-dev:48412] Updated by Victor Maslov over 2 years ago

Is it somehow related?
... .lazy ... .take_while.with_index{ ...
ArgumentError - tried to call lazy take_while without a block:`

Nevermind, swaping the chain in this way .with_index.take_while saved me.

#12 [ruby-dev:49751] Updated by Shyouhei Urabe 7 months ago

Is there reason this is not loved? I'd like to have this.

#13 [ruby-dev:49834] Updated by Shyouhei Urabe 4 months ago

  • Assignee changed from Yutaka HARA to Nobuyoshi Nakada

Also available in: Atom PDF