Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
September 29, 2014 Turn function into infinite range | ||||
---|---|---|---|---|
| ||||
Does anyone know a construct to turn a lambda into an infinite range. import std.random; unittest { Random gen; foreach(v; xxx!(() => uniform(0, 100, gen)).take(10)) writeln(v); } I though I've seen this around somewhere but can no longer find it. |
September 29, 2014 Re: Turn function into infinite range | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Nowak | On Monday, 29 September 2014 at 17:02:43 UTC, Martin Nowak wrote:
> Does anyone know a construct to turn a lambda into an infinite range.
>
> import std.random;
>
> unittest
> {
> Random gen;
> foreach(v; xxx!(() => uniform(0, 100, gen)).take(10))
> writeln(v);
> }
>
> I though I've seen this around somewhere but can no longer find it.
I can't find anything to do it. That seems weirdly absent. You can abuse recurrence to do it.
Random gen;
foreach(v; recurrence!((a, n) => uniform(0, 100, gen))(0).dropOne.take(10))
writeln(v);
|
September 29, 2014 Re: Turn function into infinite range | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Nowak | On Monday, 29 September 2014 at 17:02:43 UTC, Martin Nowak wrote:
> I though I've seen this around somewhere but can no longer find it.
AFAIK, this as never existed.
We recently merged in "cache" into phobos. This seems like a prime candidate to expand to also take a function/delegate, as on of its built-in feature is that the value of "front" is not changed until "popFront()" is called. Having it also accept a function/delegate would make sense.
The issue with *not* having that is that a "dumb" adapter would fail the:
r.front == r.front
Test. And I'm pretty sure this test is expected to pass, even for the so called "transient" ranges.
I think I'll get to it now (this week).
Thoughts?
|
September 29, 2014 Re: Turn function into infinite range | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Nowak | V Mon, 29 Sep 2014 19:02:36 +0200 Martin Nowak via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> napsáno: > Does anyone know a construct to turn a lambda into an infinite range. > > import std.random; > > unittest > { > Random gen; > foreach(v; xxx!(() => uniform(0, 100, gen)).take(10)) > writeln(v); > } > > I though I've seen this around somewhere but can no longer find it. http://dlang.org/phobos/std_range.html#.Repeat ? |
September 29, 2014 Re: Turn function into infinite range | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozák | On Monday, 29 September 2014 at 21:16:27 UTC, Daniel Kozák via Digitalmars-d-learn wrote:
> V Mon, 29 Sep 2014 19:02:36 +0200
> Martin Nowak via Digitalmars-d-learn
> <digitalmars-d-learn@puremagic.com> napsáno:
>
>> Does anyone know a construct to turn a lambda into an infinite range.
>>
>> import std.random;
>>
>> unittest
>> {
>> Random gen;
>> foreach(v; xxx!(() => uniform(0, 100, gen)).take(10))
>> writeln(v);
>> }
>>
>> I though I've seen this around somewhere but can no longer find it.
>
> http://dlang.org/phobos/std_range.html#.Repeat ?
That just repeats the value, but doesn't re-evaluate the value on every call to front/popFront.
|
September 29, 2014 Re: Turn function into infinite range | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On Monday, 29 September 2014 at 20:02:19 UTC, monarch_dodra wrote:
> I think I'll get to it now (this week).
I threw something together, and it really works exceptionally
well. Will file PR soon.
|
Copyright © 1999-2021 by the D Language Foundation