Bug #7877
closedE::Lazy#with_index should be lazy
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
Updated by nobu (Nobuyoshi Nakada) over 11 years ago
- File 0001-enumerator.c-Enumerator-Lazy-with_index.patch 0001-enumerator.c-Enumerator-Lazy-with_index.patch added
- Description updated (diff)
Updated by marcandre (Marc-Andre Lafortune) over 11 years ago
See #7696 on how to handle state...
Updated by shyouhei (Shyouhei Urabe) over 11 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 11 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 11 years ago
@marcandre (Marc-Andre Lafortune) oh, thank you! You saved my day.
Still I want this though.
Updated by zzak (zzak _) over 11 years ago
Propose to move this to next major?
Updated by duerst (Martin Dürst) over 11 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 (zzak _) over 11 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 (zzak _) over 11 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) almost 11 years ago
- Target version changed from 2.1.0 to 2.2.0
Updated by Nakilon (Victor Maslov) over 10 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 8 years ago
Is there reason this is not loved? I'd like to have this.
Updated by shyouhei (Shyouhei Urabe) about 8 years ago
- Assignee changed from yhara (Yutaka HARA) to nobu (Nobuyoshi Nakada)
Updated by jeremyevans0 (Jeremy Evans) about 5 years 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 5 years ago
- Description updated (diff)
- Assignee changed from nobu (Nobuyoshi Nakada) to jeremyevans0 (Jeremy Evans)
Thank you, I've missed it.
Updated by nobu (Nobuyoshi Nakada) about 5 years ago
- Status changed from Assigned to Closed