Last active
October 26, 2016 16:14
-
-
Save Eibwen/758169936f79d40e0471 to your computer and use it in GitHub Desktop.
C# Curry Extensions
This file contains 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
///<summary> | |
///Saw a thing about Currying in javascript frameworks, thought I'd throw the ability to do similar in C# | |
/// (Somewhere in this https://news.ycombinator.com/item?id=8652348) | |
///</summary> | |
public static class CurryExtensions | |
{ | |
//Supplying 1 value | |
public static Func<T2, TOut> Curry<T1, T2, TOut>(this Func<T1, T2, TOut> func, T1 value) | |
{ | |
return x => func(value, x); | |
} | |
public static Func<T2, T3, TOut> Curry<T1, T2, T3, TOut>(this Func<T1, T2, T3, TOut> func, T1 value) | |
{ | |
return (x, y) => func(value, x, y); | |
} | |
public static Func<T2, T3, T4, TOut> Curry<T1, T2, T3, T4, TOut>(this Func<T1, T2, T3, T4, TOut> func, T1 value) | |
{ | |
return (x, y, z) => func(value, x, y, z); | |
} | |
public static Func<T2, T3, T4, T5, TOut> Curry<T1, T2, T3, T4, T5, TOut>(this Func<T1, T2, T3, T4, T5, TOut> func, T1 value) | |
{ | |
return (x, y, z, aa) => func(value, x, y, z, aa); | |
} | |
//Supplying 2 values | |
public static Func<T3, TOut> Curry<T1, T2, T3, TOut>(this Func<T1, T2, T3, TOut> func, T1 value, T2 v2) | |
{ | |
return (x) => func(value, v2, x); | |
} | |
public static Func<T3, T4, TOut> Curry<T1, T2, T3, T4, TOut>(this Func<T1, T2, T3, T4, TOut> func, T1 value, T2 v2) | |
{ | |
return (x, y) => func(value, v2, x, y); | |
} | |
public static Func<T3, T4, T5, TOut> Curry<T1, T2, T3, T4, T5, TOut>(this Func<T1, T2, T3, T4, T5, TOut> func, T1 value, T2 v2) | |
{ | |
return (x, y, z) => func(value, v2, x, y, z); | |
} | |
//Supplying 3 values | |
public static Func<T4, TOut> Curry<T1, T2, T3, T4, TOut>(this Func<T1, T2, T3, T4, TOut> func, T1 value, T2 v2, T3 v3) | |
{ | |
return (x) => func(value, v2, v3, x); | |
} | |
public static Func<T4, T5, TOut> Curry<T1, T2, T3, T4, T5, TOut>(this Func<T1, T2, T3, T4, T5, TOut> func, T1 value, T2 v2, T3 v3) | |
{ | |
return (x, y) => func(value, v2, v3, x, y); | |
} | |
//Supplying 4 values | |
public static Func<T5, TOut> Curry<T1, T2, T3, T4, T5, TOut>(this Func<T1, T2, T3, T4, T5, TOut> func, T1 value, T2 v2, T3 v3, T4 v4) | |
{ | |
return (x) => func(value, v2, v3, v4, x); | |
} | |
} |
This file contains 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
public int Sum(int a, int b) | |
{ | |
return a + b; | |
} | |
public int Sum42(int a) | |
{ | |
Func<int, int, int> sum = Sum; | |
return sum.Curry(42)(a); | |
} | |
public int Sum42b(int a) | |
{ | |
return ((Func<int, int, int>)Sum).Curry(42)(a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment