Bug 24775 – Input range -> take -> filter -> chain: Take gets applied *after* filter

Status
NEW
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2024-09-20T13:14:28Z
Last change time
2024-12-01T16:43:00Z
Assigned to
No Owner
Creator
anon
Moved to GitHub: phobos#10561 →

Comments

Comment #0 by qigezx+dc40d6nao940k — 2024-09-20T13:14:28Z
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
Comment #1 by robert.schadek — 2024-12-01T16:43:00Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10561 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB