Skip to content

Instantly share code, notes, and snippets.

@savaged
Created March 25, 2023 17:18
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 savaged/4e711bd3c545f68f01a89ed2e910ebc0 to your computer and use it in GitHub Desktop.
Save savaged/4e711bd3c545f68f01a89ed2e910ebc0 to your computer and use it in GitHub Desktop.
Fun tasting curry in C#
// See Computerphile: https://youtu.be/psmu_VAuiag
// add x y = x + y
Func<int, int, int> AddTwoParameters = (x, y) => x + y;
// map (add 100) [1..10]
Enumerable.Range(1, 10)
.Select(i => AddTwoParameters.AddLater()(100)(i))
.ToList()
.ForEach(i => Console.WriteLine(i));
static class Curry
{
// add = \x -> (\y -> x + y)
public static Func<TInput1, Func<TInput2, TOutput>> AddLater<TInput1, TInput2, TOutput>(
this Func<TInput1, TInput2, TOutput> f)
=> x => y => f(x, y);
}
@savaged
Copy link
Author

savaged commented Mar 25, 2023

Or just...
dotnet add package Curryfy

using static Curryfy.CurryExtensions;

// add x y = x + y
Func<int, int, int> AddTwoParameters = (x, y) => x + y;

var AddTwoParametersCurried = AddTwoParameters.Curry();
Console.WriteLine(AddTwoParametersCurried(1)(2));

See https://github.com/leandromoh/Curryfy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment