Bug 16326 – filter is not lazy enough & has weird save behavior

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-07-27T14:15:37Z
Last change time
2018-01-05T13:28:49Z
Assigned to
No Owner
Creator
Eyal
Blocks
17042

Comments

Comment #0 by eyal — 2016-07-27T14:15:37Z
std.algorithm.filter's this() constructor advances the underlying range until the predicate is fulfilled. This happens *BEFORE* its next item is demanded (i.e: Not lazily). This has a number of unfortunate consequences: Firstly, it makes 'save' which uses the constructor to copy the range sensitive to mutations in the underlying range: void sensitiveToMutations() { { auto f = [1,2,3].filter!(x => x%2==0); auto g = f.save; f.front = 5; assert(g.front == 5); // works } { auto f = [1,2,3].filter!(x => x%2==0); f.front = 5; auto g = f.save; assert(g.front == 5); // fails! } } The above is reduced from actual confusing behavior we've experienced. This extra eagerness can easily cause infinite loops: void loopsForever() { import std.range : repeat; // This loops forever: auto f = repeat(1).filter!(x=>x%2 == 0); } And lastly, if the predicate is expensive or has undesirable side effects that we wish to keep to a minimum, filter is wasteful: void redundantPredicateCalls() { import std.stdio : writeln; auto veryExpensivePredicate(int x) { writeln("OUCH"); return true; } // OUCH number 1 (already redundant) auto f = [1,2,3].filter!veryExpensivePredicate; // OUCH number 2 (Redundant!) auto g = f.save; }
Comment #1 by andrei — 2017-05-14T13:10:50Z
Comment #2 by github-bugzilla — 2017-05-14T14:43:10Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/0e441a9d9e0f56b7a69d2c3fa4e157857239ee35 Fix Issue 16326 - filter is not lazy enough & has weird save behavior https://github.com/dlang/phobos/commit/d6b55f840fab8aa9981e18dae2e25e0fe71dd346 Merge pull request #5392 from andralex/16326 Fix Issue 16326 - filter is not lazy enough & has weird save behavior
Comment #3 by github-bugzilla — 2017-06-17T11:34:48Z
Commits pushed to stable at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/0e441a9d9e0f56b7a69d2c3fa4e157857239ee35 Fix Issue 16326 - filter is not lazy enough & has weird save behavior https://github.com/dlang/phobos/commit/d6b55f840fab8aa9981e18dae2e25e0fe71dd346 Merge pull request #5392 from andralex/16326
Comment #4 by github-bugzilla — 2018-01-05T13:28:49Z
Commits pushed to dmd-cxx at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/0e441a9d9e0f56b7a69d2c3fa4e157857239ee35 Fix Issue 16326 - filter is not lazy enough & has weird save behavior https://github.com/dlang/phobos/commit/d6b55f840fab8aa9981e18dae2e25e0fe71dd346 Merge pull request #5392 from andralex/16326