challenge #2: implement the varargs_reduce metafunction
Andrei Alexandrescu (See Website For Email)
SeeWebsiteForEmail at erdani.org
Tue Jan 23 10:39:42 PST 2007
Bruno Medeiros wrote:
> Andrei Alexandrescu (See Website for Email) wrote:
>> My previous post defines max by availing itself of a metafunction called
>> varargs_reduce. Your challenge is to define it. varargs_reduce takes an
>> alias which it assumes is a function of two arguments, and it defines a
>> vararg function that applies that alias repeatedly, similarly to the
>> reduce primitive that is present in functional languages. Again, for
>> background on functional reduce, see e.g.:
>>
>> http://www.joelonsoftware.com/items/2006/08/01.html
>>
>> Define varargs_reduce to deduce the result type properly, and to
>> generate code as efficient as possible.
>>
>>
>> Andrei
>
> Here is my try. I assume the reduce type must be exactly the same in all
> arguments. Should that not be the case?
That is not the case, and is actually an important point in implementing
varargs_reduce.
> As for efficiency, I'm not sure what the requirements are. Is there to
> be any parallelization/vectorization considerations? This one, instead
> of linear iteration, uses binary recursion to calculate the result, but
> for *no particular reason* since its not actually any faster than linear
> recursion.
It could well be faster. The dependency chain in your expansion has
logarithmic depth; in linear expansion it has linear depth. Superscalar
machines have the ability to execute independent operations literally in
parallel (they have multiple ALUs).
> This one seems much more easy that the fist challenge, so I suspect I'm
> missing something.
As Christian Kamm mentioned, your solution does not work with templates,
and min2 actually is one.
Andrei
More information about the Digitalmars-d
mailing list