Skip to content

Instantly share code, notes, and snippets.

@gshutler
Created April 8, 2010 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gshutler/360105 to your computer and use it in GitHub Desktop.
Save gshutler/360105 to your computer and use it in GitHub Desktop.
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;
}
}
}
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