Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input range -> take -> filter -> chain: Take gets applied *after* filter #10561

Open
dlangBugzillaToGithub opened this issue Sep 20, 2024 · 0 comments

Comments

@dlangBugzillaToGithub
Copy link

qigezx+dc40d6nao940k reported this on 2024-09-20T13:14:28Z

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

Description

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
@thewilsonator thewilsonator removed OS:Linux Issues specific to Linux Arch:x86_64 Issues specific to x86_64 P1 labels Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants