Created
April 8, 2010 14:13
-
-
Save gshutler/360105 to your computer and use it in GitHub Desktop.
Code listing for http://blog.robustsoftware.co.uk/2010/04/currying-with-c.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Currying | |
{ | |
public static class Fn | |
{ | |
public static Func<Func<int>, Func<int>> Curry(Func<Func<int>, Func<int>> x, Func<Func<int>, Func<int>> y) | |
{ | |
return a => y(x(a)); | |
} | |
public static Func<int> Sum(Func<int> x, Func<int> y) | |
{ | |
return Literal(x() + y()); | |
} | |
public static Func<Func<int>, Func<int>> Sum(Func<int> x) | |
{ | |
return y => Sum(x, y); | |
} | |
public static Func<int> Multiply(Func<int> x, Func<int> y) | |
{ | |
return Literal(x() * y()); | |
} | |
public static Func<Func<int>, Func<int>> Multiply(Func<int> x) | |
{ | |
return y => Multiply(x, y); | |
} | |
public static Func<int> Literal(int x) | |
{ | |
return () => x; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Currying | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
Func<Func<int>, Func<int>> addTwo = Fn.Sum(Fn.Literal(2)); | |
Func<int> three = addTwo(Fn.Literal(1)); | |
Func<Func<int>, Func<int>> timesThree = Fn.Multiply(Fn.Literal(3)); | |
Func<Func<int>, Func<int>> addTwoTimesThree = Fn.Curry(addTwo, timesThree); | |
Func<int> twentyOne = addTwoTimesThree(Fn.Literal(5)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment