Function literals -- strange behavior

Justin mrjnewt at hotmail.com
Thu Dec 4 12:08:51 PST 2008


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;
}




More information about the Digitalmars-d mailing list