Woldemort Type can't even call "take" or "array"

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 31 01:02:09 PDT 2015


On 05/31/2015 12:39 AM, kerdemdemir wrote:
> And one more question out of curiosity why I couldn't find any method
> which will return the max element in a range.

I am not sure but perhaps because it is easily implemented by reduce:

import std.stdio;
import std.algorithm;
import std.range;
import std.random;
import std.format;
import std.conv;

auto maxElement(R)(R range)
{
     return reduce!((current, a) => current >= a ? current : a)
                   (ElementType!R.min, range);
}

void main()
{
     auto numArr = 100
                   .iota
                   .randomSample(20, Random(unpredictableSeed))
                   .array;

     randomShuffle(numArr);

     auto result = numArr.maxElement;

     writefln("Max: %s", result);

     writefln("%-(%s %)", numArr.map!(a => (a == result
                                            ? format("((%s))", a)
                                            : a.to!string)));
}

One output:

Max: 98
93 16 74 94 23 ((98)) 9 73 71 77 8 11 36 55 66 46 42 44 58 5

Ali



More information about the Digitalmars-d-learn mailing list