Truly lazy ranges, transient .front, and std.range.Generator
HaraldZealot via Digitalmars-d
digitalmars-d at puremagic.com
Tue Aug 18 01:59:19 PDT 2015
On Saturday, 15 August 2015 at 10:06:13 UTC, Joseph Rushton
Wakeling wrote:
> ...
>
> In some cases we're going to want true laziness of evaluation,
> but _not_ transience of .front. In these cases, the _first_
> time .front is called, its value should be freshly evaluated;
> but thereafter, the value is cached, until .popFront() is
> called, at which point .front will be re-evaluated lazily the
> next time it's called. Something like std.algorithm.cache is
> inappropriate here, precisely because it's eager in its
> calculation of the cached values.
>
> ...
>
> -- Joe
Do you mean something like that:
```d
struct Range
{
public:
enum empty = false;
auto ref front() @property
{
if(mustBeEvaluated)
{
cache = fun();
mustBeEvaluated = false;
}
return cache;
}
void popFront()
{
mustBeEvaluated = true;
}
private:
ReturnType!fun cache;
bool mustBeEvaluated = true;
}
```
?
More information about the Digitalmars-d
mailing list