[Issue 16375] New: Computing finite ranges with std.range.recurrence

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Aug 11 06:58:49 PDT 2016


https://issues.dlang.org/show_bug.cgi?id=16375

          Issue ID: 16375
           Summary: Computing finite ranges with std.range.recurrence
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: jens.k.mueller at gmx.de

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
}

--


More information about the Digitalmars-d-bugs mailing list