www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16375] New: Computing finite ranges with std.range.recurrence


          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 puremagic.com
          Reporter: jens.k.mueller 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.

    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

    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

Aug 11 2016