Trying Multiple Aggregate reduce

bearophile bearophileHUGS at lycos.com
Mon Mar 31 15:13:08 PDT 2014


Nordlöw:

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

import std.algorithm: min, max, reduce;
import std.typecons: tuple, Unqual;
import std.range: isInputRange, ElementType;

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

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

void main() {}


> 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/

You can't use reduce with a const seed.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list