Bug 16375 – std.range.recurrence should be lazy

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2016-08-11T13:58:49Z
Last change time
2019-12-18T08:41:40Z
Assigned to
No Owner
Creator
jens.k.mueller
Blocks
17042

Comments

Comment #0 by jens.k.mueller — 2016-08-11T13:58:49Z
Assume you have a recurrence that is finite, i.e., you know how to compute the next value from previous values but in total there are only finite many values. To me std.range.recurrence looks like a perfect fit to tackle the problem. But it turns out recurrence does not work with finite ranges when you access the last element. unittest { static auto next(R)(R states, size_t n) { if (n <= 1) return states[n - 1] + 1; // recurrence with finite elements // only two elements in this case assert(false); } import std.range : recurrence, take; import std.algorithm : count; auto firstNumbers = recurrence!next(0); assert(firstNumbers.take(2).count() == 2); // fails // because when accessing an element of a recurrence the next // element is computed; what if there is no next element, i.e., the // recurrence range is finite }
Comment #1 by greeenify — 2016-12-27T13:10:10Z
> But it turns out recurrence does not work with finite ranges when you access the last element. So it should be lazy in regards to popFront() doing no extra work.
Comment #2 by bugzilla — 2019-12-18T08:41:40Z
According to run.dlang.io this has been fixed since version 2.076.1.