Monday 28 March 2011

C# 2.0 - Functional composition and closures

Sadly, it seems that anonymous functions / delegates cannot hold state so although we can imitate closures if the delegate is defined inline, we cannot do the below:

int diff = 2;
Functional.filter2_fn<int, int> f = delegate(int a, int b) { return Math.Abs(a - b) > diff; };
diff = 3;
bool retval = Functional.forAll2(f, l, m);

because, by the time we come to use
f
, the value stored in
diff
has changed to 3.

No comments:

Post a Comment