Project

General

Profile

Bug #7877

E::Lazy#with_index should be lazy

Added by shyouhei (Shyouhei Urabe) over 6 years ago. Updated 15 days ago.

Status:
Closed
Priority:
Normal
Target version:
-
ruby -v:
2.1.0-dev
[ruby-dev:47025]

Description

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(:+)
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.


Files

0001-enumerator.c-Enumerator-Lazy-with_index.patch (3.33 KB) 0001-enumerator.c-Enumerator-Lazy-with_index.patch nobu (Nobuyoshi Nakada), 02/19/2013 03:25 PM
lazy-with-index-block-7877.patch (4.08 KB) lazy-with-index-block-7877.patch jeremyevans0 (Jeremy Evans), 08/08/2019 08:43 PM

Associated revisions

Revision 83498854
Added by jeremyevans (Jeremy Evans) 17 days ago

Make Enumerator::Lazy#with_index be lazy

Previously, Enumerator::Lazy#with_index was not defined, so it
picked up the default implementation from Enumerator, which was
not lazy.

Based on earlier patch from nobu.

Fixes [Bug #7877]

Revision e94ac03e
Added by jeremyevans (Jeremy Evans) 15 days ago

Make Enumerator::Lazy#with_index be lazy

Previously, Enumerator::Lazy#with_index was not defined, so it
picked up the default implementation from Enumerator, which was
not lazy.

Based on earlier patch from nobu.

Fixes [Bug #7877]

History

Updated by marcandre (Marc-Andre Lafortune) over 6 years ago

See #7696 on how to handle state...

Updated by shyouhei (Shyouhei Urabe) over 6 years ago

  • Description updated (diff)

OK, so marcandre (Marc-Andre Lafortune) is interested in. I re-wrote the description in English.

Updated by marcandre (Marc-Andre Lafortune) over 6 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).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.

Updated by shyouhei (Shyouhei Urabe) over 6 years ago

marcandre (Marc-Andre Lafortune) oh, thank you! You saved my day.

Still I want this though.

Updated by zzak (Zachary Scott) over 6 years ago

Propose to move this to next major?

Updated by duerst (Martin Dürst) over 6 years 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.

Updated by zzak (Zachary Scott) over 6 years ago

duerst (Martin Dürst) 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

Updated by zzak (Zachary Scott) over 6 years 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 2.6 to 2.1.0
  • ruby -v set to 2.1.0-dev
  • Backport set to 2.0.0: UNKNOWN

Updated by hsbt (Hiroshi SHIBATA) over 5 years ago

  • Target version changed from 2.1.0 to 2.2.0

Updated by Nakilon (Victor Maslov) about 5 years ago

Is it somehow related? http://stackoverflow.com/q/24782712/322020
... .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.

Updated by shyouhei (Shyouhei Urabe) about 3 years ago

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

Updated by shyouhei (Shyouhei Urabe) almost 3 years ago

  • Assignee changed from yhara (Yutaka HARA) to nobu (Nobuyoshi Nakada)

Updated by jeremyevans0 (Jeremy Evans) about 1 month ago

This bug is still present in the master branch. I've updated nobu's patch to apply to the master branch, which required a significant rewrite. The updated patch is attached.

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

  • Assignee changed from nobu (Nobuyoshi Nakada) to jeremyevans0 (Jeremy Evans)
  • Description updated (diff)

Thank you, I've missed it.

#16

Updated by nobu (Nobuyoshi Nakada) 15 days ago

  • Status changed from Assigned to Closed

Also available in: Atom PDF