[Issue 4705] Redesign of std.algorithm.max()/min() + mins()/maxs()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Apr 14 14:23:41 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=4705



--- Comment #16 from bearophile_hugs at eml.cc 2013-04-14 14:23:35 PDT ---
I still think mins()/maxs() are useful. But years after the original proposal
an API change in max/min is now problematic (unless you want to introduce
maximum/minimum functions). So I propose a small change in my original request.

Now I suggest to keep the max/min functions diadic as they are now, and add the
optional 'key' function. This is a backwards-compatible change in max/min.

This is the updated example code presented here:
http://d.puremagic.com/issues/show_bug.cgi?id=4705#c5


import std.stdio, std.algorithm, std.range, std.typecons;

auto hailstone(int n) pure nothrow {
    auto result = [n];
    while (n != 1) {
        n = (n & 1) ? (n * 3 + 1) : (n / 2);
        result ~= n;
    }
    return result;
}

void main() {
    iota(1, 1000)
    .map!(i => tuple(i.hailstone.length, i))
    .reduce!max[1]
    .writeln; // 871
}


With the original proposal the main() becomes (with the maximum the code is
very similar):

void main() {
    iota(1, 1000)
    max!(i => i.hailstone.length)
    .writeln;
}



With the reduced proposal the main() becomes:

void main() {
    iota(1, 1000)
    reduce!(max!(i => i.hailstone.length))
    .writeln;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list