Thread overview
Strange output
Jun 07, 2013
Daemon
Jun 07, 2013
Daemon
Jun 07, 2013
develop32
Jun 07, 2013
Daemon
Jun 07, 2013
Jesse Phillips
June 07, 2013
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.
June 07, 2013
> auto de = find!(delegate(a) { return a < 5; })([10, 11, 15, 16, 27, 20, 2, -4, -17, 8, 64, 6]);

Just a clarification, it prints out 64 with the input above, I changed it later just to test it and forgot to update the rest.

June 07, 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.

June 07, 2013
On Friday, 7 June 2013 at 19:47:39 UTC, develop32 wrote:
> 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.

I totally misunderstood the for loop before, I feel ashamed. Thanks a lot, haha
June 07, 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.

You're probably just playing around, but FYI the function is

http://dlang.org/phobos/std_algorithm.html#filter

While you've created the same find already in std.algorithm.