[Issue 24775] New: Input range -> take -> filter -> chain: Take gets applied *after* filter

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Sep 20 13:14:28 UTC 2024


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

          Issue ID: 24775
           Summary: Input range -> take -> filter -> chain: Take gets
                    applied *after* filter
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: qigezx+dc40d6nao940k at grr.la

InputRange.take(n).filter.chain logically transforms it into
InputRange.filter.take(n-1).chain and thus takes the wrong number of elements

Example:
----
import std.stdio;
import std.algorithm;
import std.range;

auto inputRangeFactory(){
        int i = 0;
        int gen(){
                return i++;
        }
        return generate!gen();
}

void main(){

        // [10, 11, 12, 13, 14, 15, 16, 17, 18, 100]
        inputRangeFactory.take(10).filter!"a>8".chain(only(100)).writeln;

        // Unless filter would discard all taken elements
        // [100]
        inputRangeFactory.take(10).filter!"a>9".chain(only(100)).writeln;

        // Adding "array" somewhere in the middle fixes it
        // [9,100]
        inputRangeFactory.take(10).array.filter!"a>8".chain(only(100)).writeln;
        inputRangeFactory.take(10).filter!"a>8".array.chain(only(100)).writeln;
}
------

$ dmd --version
DMD64 D Compiler v2.109.1

Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved
written by Walter Bright

--


More information about the Digitalmars-d-bugs mailing list