D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10845 - std.range.Cycle broken for reference type forward ranges
Summary: std.range.Cycle broken for reference type forward ranges
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-08-18 04:59 UTC by Peter Alexander
Modified: 2014-01-25 14:20 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Peter Alexander 2013-08-18 04:59:56 UTC
import std.stdio;
import std.range;
import std.algorithm;

void main()
{
    auto a = inputRangeObject(iota(3).filter!"true");
    writeln(a.cycle.take(10));
}

Outputs

[0, 1, 2, 0, 1, 2,

then asserts: http://dpaste.dzfl.pl/84de028d

The problem is Cycle.popFront:

void popFront()
{
    _current.popFront();
    if (_current.empty) _current = _original;
}

It should be _current = _original.save, otherwise the _original range is consumed on the second iteration.
Comment 1 monarchdodra 2013-08-18 05:47:49 UTC
(In reply to comment #0)
> import std.stdio;
> import std.range;
> import std.algorithm;
> 
> void main()
> {
>     auto a = inputRangeObject(iota(3).filter!"true");
>     writeln(a.cycle.take(10));
> }
> 
> Outputs
> 
> [0, 1, 2, 0, 1, 2,
> 
> then asserts: http://dpaste.dzfl.pl/84de028d
> 
> The problem is Cycle.popFront:
> 
> void popFront()
> {
>     _current.popFront();
>     if (_current.empty) _current = _original;
> }
> 
> It should be _current = _original.save, otherwise the _original range is
> consumed on the second iteration.

I have an open pull for improving cycle:
https://github.com/D-Programming-Language/phobos/pull/1149

So I integrated the fix in it. That said, given how often my pulls get reviewed and pulled, it might be best if you created a pull for this that _I_ could review and pull...