make (a < b < c) illegal?

Derek Parnell derek at psych.ward
Thu Feb 8 03:41:22 PST 2007


On Thu, 08 Feb 2007 12:27:58 +0100, Tom S wrote:

> Michiel wrote:
>>> I give up. I can't work out how to create this functionality
>>> for an arbitary number of arguments.
>> 
>> You need to use recursion, not loops. I'm not sure how the syntax works exactly,
>> but it might look something like this:
>> 
>> bool ordered(T1, T2, Trest ...)(T1 first, T2 second, Trest rest) {
>>     return (first < second) && ordered(second, rest);
>> }
> 
> How about:
> 
> 
> bool ascending(T ...)(T t) {
> 	static if (t.length > 0) {
> 		foreach (i, x; t[0..$-1]) {
> 			if (x >= t[i+1]) return false;
> 		}
> 	}
> 	return true;
> }
> 
> 
> void main() {
> 	assert (ascending(1, 2, 3, 4));
> 	assert (ascending(1, 2));
> 	assert (ascending(1.2, 2.1, 3.5, 4.7, 8.6));
> 	assert (!ascending(1.2, 1.0));
> 	assert (!ascending(1.2, 1.5, 0.5));
> 	assert (ascending(1));
> 	assert (ascending());
> }

Yes this works, but I don't know why. Is the 'foreach' executed at compile
time or run time? If compile time, where is this documented? Its not in the
template docs, nor the conditional compilation page, or on the statements
page. 


-- 
Derek Parnell
Melbourne, Australia
"Justice for David Hicks!"
skype: derek.j.parnell



More information about the Digitalmars-d mailing list