make (a < b < c) illegal?

Tom S h3r3tic at remove.mat.uni.torun.pl
Thu Feb 8 03:27:58 PST 2007


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());
}


?


--
Tomasz Stachowiak



More information about the Digitalmars-d mailing list