challenge: implement the max function
Jari-Matti Mäkelä
jmjmak at utu.fi.invalid
Tue Jan 23 03:58:50 PST 2007
Frits van Bommel kirjoitti:
> Don Clugston wrote:
>> Pared down to the minimum, it is now:
>>
>> template maxtype(T...){
>> static if(T.length == 1) alias typeof(T[0]) maxtype;
>> else static if(T.length > 2)
>> alias maxtype!(maxtype!(T[0..1]), T[2..$]) maxtype;
>> else static if(is( typeof( (T[0] x, T[1] y){ return y > x ? y : x;})
>> Q == return))
>> alias Q maxtype;
>> }
>>
>> maxtype!(T) max(T...)(T arg){
>> static if(arg.length == 1) return arg[0];
>> else return arg[0] >= arg[1] ? arg[0] : max(arg[1..$]);
>> }
>>
> [snip min]
>>
>> Not too terrible.
>
> Except you broke it ;)
In fact I broke it first and mislead Don :(
> 'max' should be this:
> -----
> maxtype!(T) max(T...)(T arg){
> static if(arg.length == 1) return arg[0];
> else return max(arg[0] >= arg[1] ? arg[0] : arg[1], arg[2..$]);
> }
Just out of curiosity, what is happening here? If I write
else return arg[0] >= arg[1] ? max(arg[0]) : max(arg[1], arg[2..$]);
it does not work, but
else return max(arg[0] >= arg[1] ? arg[0] : arg[1], arg[2..$]);
works.
More information about the Digitalmars-d
mailing list