Justin wrote:
Ahh, I thought it would some stupid oversight on my part. This works:

module functionliteral;

import std.stdio;

static void main() {

        int[] values = [1,2,4,8];
        writefln(Reduce(values, function int(int x, int y) { return x + y; }));
        writefln(Reduce(values, function int(int x, int y) { return x * y; }));
}

static int Reduce(int[] values, int function(int x, int y) operation) {
        int total = values[0];
        foreach (int v; values[1..$])
                total = operation(total,v);
        return total;
}


You don't need to make those static, by the way.

Reduce doesn't work on all inputs:
writefln(Reduce([], (int x, int y) { return x + y; }));

Reply via email to