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

Jack Stouffer <j...@jackstouffer.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #3 from Jack Stouffer <j...@jackstouffer.com> ---
Using side effects in range code is a recipe for disaster. From the
std.range.isInputRange docs,

>The following are rules of input ranges are assumed to hold true in all Phobos 
>code. These rules are not checkable at compile-time, so not conforming to 
>these rules when writing ranges or range based code will result in undefined 
>behavior.
>...
>r.front evaluated multiple times, without calling r.popFront, or otherwise 
>mutating the range object or the underlying data, yields the same result for 
>every evaluation.

This code breaks that assumption, as map does not cache the results of the
lambda.

However this works,

import std.stdio, std.algorithm, std.array;

static a = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]; 

void main()
{
    size_t idx = 0;
    writeln(a.map!(s => s ~ a[idx + 1 == a.length ? idx :
++idx]).cache.joiner(", "));
}

Closing as invalid.

--

Reply via email to