Create a gist now

Instantly share code, notes, and snippets.

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