www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Turn function into infinite range

reply Martin Nowak <code dawg.eu> writes:
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.
Sep 29 2014
next sibling parent "Brad Anderson" <eco gnuk.net> writes:
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);
Sep 29 2014
prev sibling next sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
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?
Sep 29 2014
parent "monarch_dodra" <monarchdodra gmail.com> writes:
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.
Sep 29 2014
prev sibling parent reply Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
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.
Sep 29 2014
parent "monarch_dodra" <monarchdodra gmail.com> writes:
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.
That just repeats the value, but doesn't re-evaluate the value on every call to front/popFront.
Sep 29 2014