Last active
June 3, 2022 18:21
-
-
Save Grohden/9005f5452700da6809285e13a202c2b3 to your computer and use it in GitHub Desktop.
Dart currying
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
void main() { | |
final sum2 = curry2((int a, int b) => a + b); | |
final sum3 = curry3((int a, int b, int c) => a + b + c); | |
print(sum2(1)); | |
print(sum2(1)(2)); | |
print(sum3(1)); | |
print(sum3(1)(2)); | |
print(sum3(1)(2)(3)); | |
} | |
typedef Curry2<R, T1, T2> = R Function(T2) Function(T1); | |
Curry2<R, T1, T2> curry2<R, T1, T2>(R Function(T1, T2) fn) { | |
return (T1 t1) => (T2 t2) => fn(t1, t2); | |
} | |
typedef Curry3<R, T1, T2, T3> = Curry2<R Function(T3), T1, T2>; | |
Curry3<R, T1, T2, T3> curry3<R, T1, T2, T3>(R Function(T1, T2, T3) fn) { | |
return curry2((a, b) => (c) => fn(a, b, c)); | |
} | |
typedef Curry4<R, T1, T2, T3, T4> = Curry3<R Function(T4), T1, T2, T3>; | |
Curry4<R, T1, T2, T3, T4> curry4<R, T1, T2, T3, T4>(R Function(T1, T2, T3, T4) fn) { | |
return curry3((a, b, c) => (d) => fn(a, b, c, d)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment