www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23094] New: std.range.generate calls fun more than necessary

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

          Issue ID: 23094
           Summary: std.range.generate calls fun more than necessary
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: remi.thebault outlook.com

The following unit test fails (i == 2):

unittest
{
    import std.array;
    import std.random;
    import std.range;

    int someFun()
    {
        return uniform(0, 2000);
    }

    int i = 0;

    const arr = generate!(() {
        ++i;
        return someFun();
    }).take(1).array;

    assert(arr.length == 1);
    assert(i == 1, "generate called fun more than once");
}

--
May 05 2022