Trying Multiple Aggregate reduce

"Nordlöw" per.nordlow at gmail.com
Mon Mar 31 14:53:11 PDT 2014


I'm trying to figure out how to use reduce with multiply funs.

Here's my try on minmaxElement:


import std.typecons: tuple;

/** Returns: Tuple of Minmum and Maximum Element in X. */
auto minmaxElement(alias F = min, alias G = max, R)(in R range)
     @safe pure nothrow if (isInputRange!R)
{
     return reduce!(F, G)(tuple(ElementType!R.max,
                                ElementType!R.min), range);
}

unittest { assert([1, 2, 3].minmaxElement == tuple(1, 3)); }


which errors as

/home/per/opt/x86_64-unknown-linux-gnu/dmd/bin/../include/d2/std/algorithm.d(774,29): 
Error: can only initialize const member _expand_field_0 inside 
constructor
/home/per/opt/x86_64-unknown-linux-gnu/dmd/bin/../include/d2/std/algorithm.d(774,29): 
Error: can only initialize const member _expand_field_1 inside 
constructor
algorithm_ex.d(157,25): Error: template instance 
std.algorithm.reduce!(min, max).reduce!(Tuple!(const(int), 
const(int)), const(int)[]) error instantiating
algorithm_ex.d(160,28):        instantiated from here: 
minmaxElement!(min, max, const(int)[])


If I replace ElementType!R.min and ElementType!R.max with a 
values, say 0 and 1 it compiles. What is going on here?

This seems related:
http://forum.dlang.org/thread/bug-10408-3@http.d.puremagic.com/issues/


More information about the Digitalmars-d-learn mailing list