bool fn<B, C>(B b, C c) { return b.Equals(c); } Functional.fn<C, bool> curried_fn<B, C>(B b) { return delegate(C c) { return fn(b, c); }; } [Test] public void CurriedFnTest1() { bool test1a = fn(1, 2); bool test1b = curried_fn<int, int>(1)(2); Assert.AreEqual(test1a, test1b); } Functional.fn<int, int> curried_adder_int(int c) { return delegate(int p) { return FunctionalHelpers.adder_int(c, p); }; } [Test] public void CurriedFnTest2() { int[] a = { 1, 2, 3, 4, 5 }; List<int> b = Functional.map<int, int>(delegate(int a1) { return FunctionalHelpers.adder_int(2, a1); }, a); List<int> c = Functional.map<int, int>(curried_adder_int(2), a); Assert.IsTrue(CollectionComparer.SetEqual(b, c)); }
Wednesday, 20 April 2011
Curried functions in C# 2.0
This wasn't as difficult as I thought it might have been. Below I reproduce two examples of currying in C#. Easy!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment