[Issue 13958] RangeError with impure map
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Tue Feb 7 10:55:49 PST 2017
https://issues.dlang.org/show_bug.cgi?id=13958
Jack Stouffer <jack at jackstouffer.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution|--- |INVALID
--- Comment #3 from Jack Stouffer <jack at 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.
--
More information about the Digitalmars-d-bugs
mailing list