Strange output

develop32 develop32 at gmail.com
Fri Jun 7 12:47:37 PDT 2013


On Friday, 7 June 2013 at 19:10:54 UTC, Daemon wrote:
> The following program is supposed to print out only numbers 
> that are less than 5, yet the number 63 gets printed.
>
> module main;
>
> import std.stdio;
> import std.conv;
>
> int main(string[] argv)
> {
> 	auto de = find!(delegate(a) { return a < 5; })([10, 11, 15, 
> 16, 27, 20, 2, -4, -17, 8, 64, 6]);
>
> 	foreach (int b; de[0 .. $])
> 	{
> 		writeln(b);
> 	}
> 	
> 	readln();
>
>     return 0;
> }
>
> T[] find(alias pred, T)(T[] input)
> 	if (is(typeof(pred(input[0])) == bool))
> {
> 	for (; input.length > 0; input = input[1 .. $])
> 	{
> 		if (pred(input[0]))
> 		{
> 			break;
> 		}
> 	}
> 	return input;
> }
>
> I realize it's extremely improbable that I've discovered a bug, 
> so what exactly could I be doing wrong? Why is the output:
>
> 2
> -4
> -17
> 8
> 63
> 6
>
> I'm probably missing something obvious. If the input is just 
> [63], then nothing gets printed out.

It looks like your find function just stops at the first item 
that is less than five (which is two) and returns a range 
consisting of all the following items.
If you'll look closer, your output is just all items after and 
including 2.



More information about the Digitalmars-d-learn mailing list