Skip to content

Commit

Permalink
Curry/Uncurry extension
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroMaglione committed Jul 5, 2021
1 parent de6f906 commit e0f5961
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# v0.0.7

- Added `Compose` and `Compose2`, used to easily compose functions in a chain.
- Added `curry` and `uncurry` extensions on functions up to 5 parameters.

# v0.0.6 - 29 June 2021

Expand Down
9 changes: 9 additions & 0 deletions example/src/function/curry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ void main() {
final divideBy3 = multiplyBy2(3);
print(divideBy3(10));
print(sumMultiplyDivideCurry(5)(2)(3)(10));

/// Using the extension
final sumBy2Extension = sum.curry(2);
final sumBy10Extension = sum.curry(10);
print(sumBy2Extension(10));
print(sumBy10Extension(2));

final fourParamsCurry = sumMultiplyDivide.curry;
final fourParamsUncurry = fourParamsCurry.uncurry;
}
91 changes: 91 additions & 0 deletions lib/src/function.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,94 @@ F Function(A a, B b, C c, D d, E e) uncurry5<A, B, C, D, E, F>(
function) {
return (A a, B b, C c, D d, E e) => function(a)(b)(c)(d)(e);
}

// --- Extensions below 👇 --- //

extension CurryExtension2<Input1, Input2, Output> on Output Function(
Input1, Input2) {
/// Convert this function from accepting two parameters to a function
/// that returns another function both accepting one parameter.
///
/// Inverse of `uncurry`.
Output Function(Input2) curry(Input1 input1) =>
(input2) => this(input1, input2);
}

extension UncurryExtension2<Input1, Input2, Output> on Output Function(Input2)
Function(Input1) {
/// Convert a function that returns another function to a single function
/// accepting two parameters.
///
/// Inverse of `curry`.
Output Function(Input1, Input2) uncurry() =>
(Input1 input1, Input2 input2) => this(input1)(input2);
}

extension CurryExtension3<Input1, Input2, Input3, Output> on Output Function(
Input1, Input2, Input3) {
/// Convert this function from accepting three parameters to a series
/// of functions that all accept one parameter.
///
/// Inverse of `uncurry`.
Output Function(Input3) Function(Input2) curry(Input1 input1) =>
(input2) => (input3) => this(input1, input2, input3);
}

extension UncurryExtension3<Input1, Input2, Input3, Output>
on Output Function(Input3) Function(Input2) Function(Input1) {
/// Convert a function that returns a series of functions to a single function
/// accepting three parameters.
///
/// Inverse of `curry`.
Output Function(Input1, Input2, Input3) uncurry() =>
(Input1 input1, Input2 input2, Input3 input3) =>
this(input1)(input2)(input3);
}

extension CurryExtension4<Input1, Input2, Input3, Input4, Output> on Output
Function(Input1, Input2, Input3, Input4) {
/// Convert this function from accepting four parameters to a series
/// of functions that all accept one parameter.
///
/// Inverse of `uncurry`.
Output Function(Input4) Function(Input3) Function(Input2) curry(
Input1 input1) =>
(input2) => (input3) => (input4) => this(input1, input2, input3, input4);
}

extension UncurryExtension4<Input1, Input2, Input3, Input4, Output>
on Output Function(Input4) Function(Input3) Function(Input2) Function(
Input1) {
/// Convert a function that returns a series of functions to a single function
/// accepting four parameters.
///
/// Inverse of `curry`.
Output Function(Input1, Input2, Input3, Input4) uncurry() =>
(Input1 input1, Input2 input2, Input3 input3, Input4 input4) =>
this(input1)(input2)(input3)(input4);
}

extension CurryExtension5<Input1, Input2, Input3, Input4, Input5, Output>
on Output Function(Input1, Input2, Input3, Input4, Input5) {
/// Convert this function from accepting five parameters to a series
/// of functions that all accept one parameter.
///
/// Inverse of `uncurry`.
Output Function(Input5) Function(Input4) Function(Input3) Function(Input2)
curry(Input1 input1) => (input2) => (input3) =>
(input4) => (input5) => this(input1, input2, input3, input4, input5);
}

extension UncurryExtension5<Input1, Input2, Input3, Input4, Input5, Output>
on Output Function(Input5) Function(Input4) Function(Input3) Function(
Input2)
Function(Input1) {
/// Convert a function that returns a series of functions to a single function
/// accepting five parameters.
///
/// Inverse of `curry`.
Output Function(Input1, Input2, Input3, Input4, Input5) uncurry() =>
(Input1 input1, Input2 input2, Input3 input3, Input4 input4,
Input5 input5) =>
this(input1)(input2)(input3)(input4)(input5);
}

0 comments on commit e0f5961

Please sign in to comment.