D Recurrences

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Jun 9 10:14:36 PDT 2011


On 6/9/11 12:06 PM, Ben Grabham wrote:
> Also, how come recurrence isn't properly lazy?
> If I define a recurrence and iterate over it twice with foreach, it
> takes the same amount of time due to the stack size being set. Is there
> a way of defining a lazy list that stores the results when calculated?

recurrence() is very simple: it starts with the state you pass to it. 
Then each popFront() overwrites the head of the state (heh) with the 
calculated value from the existing state.

I agree that one could delay any computation until the initial state is 
exhausted - for example, if the state size is 100, then there's no need 
to do anything for the first 99 steps. That's far from a common case 
however as most recurrences have small states and are iterated numerous 
times. The necessary bookkeeping would slow down that common case a bit.


Andrei


More information about the Digitalmars-d mailing list