SCurry 1.0.0

Currying and Partial application for C# functions and actions.

There is a newer version of this package available.
See the version list below for details.
Install-Package SCurry -Version 1.0.0
dotnet add package SCurry --version 1.0.0
<PackageReference Include="SCurry" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SCurry --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Currying C# functions and actions

If you are here, you probably know what Curry and Partial Application are. This library contains extensions for all standard Func<> and Action<> delegates.

Build status
codecov
NuGet Badge

Features

  1. Supports all standard Func<> and Action<> delegates.
  2. Curry and Partial Application with special spacer.
  3. Uncurrying.
  4. Pipe and Compose helper functions.
  5. All code is auto generated and covered by unit tests.
  6. Based on .NET Standard 2.0
  7. No external dependencies.

Examples

Lets say you have the next function:

Func<int, int, int, int> Multiply = (a, b, c) => a * b * c;

Curry

Now you can curry it:

Func<int, Func<int, Func<int, int>>> multiplyCurried = Multiply.Curry();
int result = multiplyCurried(2)(3)(4); // 24

In case if you know the values for some arguments in advance you can use the spacer:

using static SCurry.Spacer;
...
Func<int, int> multiply6 = Multiply.Curry(3, _, 2); // or Multiply.Curry(_, _, 6) or or Multiply.Curry(_, 6)
int result = multiply6(5); // 30

At the moment you can use the spacer for delegates with up to 6 parameters. Also, as crazy as it sounds, the library supports "partial application" for curry. You can define first arguments for delegates with up to 8 parameters:

Func<int, int, int, int, int, int, int, int, int> Multiply8 = (a, b, c, d, e, f, g, h) => a * b * c * d * e * f * g * h;
...
Func<int, int> curried = Multiply8.Curry(1, 2, 3, 4, 5, 6);
int result = curried(7)(8); // 40320

Uncurry

After curry you may want to get normal function back:

Func<int, Func<int, int>> multiply2curryed = Multiply.Curry(_, 2);
Func<int, int, int> multiply2 = multiply2curryed.Uncurry();
int result = multiply2(3, 4); // 24

Partial Application

The library has Partial Application extensions for all standard delegates:

Func<int, int, int> multiply2Partial = Multiply.Partial(2);
int result = multiply2Partial(3, 4); // 24

Also at the moment you can use the spacer for delegates with up to 6 parameters:

Func<int, int, int> multiply2Partial = Multiply.Partial(_, _, 2);
int result = multiply2Partial(3, 4); // 24

Pipe and Compose

You can chain functions with Pipe:

using static SCurry.Piper;
...
Func<int, int, int, int> Multiply = (a, b, c) => a * b * c;
Func<int, int> CurryedAdd2WithGap = TestFunctions.Add2.Curry(_, 2);
Func<int, Func<int, int>> CurryedPartialMultiplyBy3 = Multiply.Partial(_, _, 3).Curry();
Func<int, Func<int, Func<int, int>>> CurryedAdd3 = TestFunctions.Add3.Curry();

var piped = Pipe(
    CurryedAdd2WithGap,
    CurryedPartialMultiplyBy3(1),
    CurryedAdd3(3)(1)
);

var result = piped(0); // 10

Or Compose them:

using static SCurry.Composer;
...
var piped = Compose(
    CurryedAdd2WithGap,
    CurryedPartialMultiplyBy3(1),
    CurryedAdd3(3)(1)
);

var result = piped(0); // 14

Installation

PM> Install-Package SCurry
dotnet add package SCurry

Links

Currying C# functions and actions

If you are here, you probably know what Curry and Partial Application are. This library contains extensions for all standard Func<> and Action<> delegates.

Build status
codecov
NuGet Badge

Features

  1. Supports all standard Func<> and Action<> delegates.
  2. Curry and Partial Application with special spacer.
  3. Uncurrying.
  4. Pipe and Compose helper functions.
  5. All code is auto generated and covered by unit tests.
  6. Based on .NET Standard 2.0
  7. No external dependencies.

Examples

Lets say you have the next function:

Func<int, int, int, int> Multiply = (a, b, c) => a * b * c;

Curry

Now you can curry it:

Func<int, Func<int, Func<int, int>>> multiplyCurried = Multiply.Curry();
int result = multiplyCurried(2)(3)(4); // 24

In case if you know the values for some arguments in advance you can use the spacer:

using static SCurry.Spacer;
...
Func<int, int> multiply6 = Multiply.Curry(3, _, 2); // or Multiply.Curry(_, _, 6) or or Multiply.Curry(_, 6)
int result = multiply6(5); // 30

At the moment you can use the spacer for delegates with up to 6 parameters. Also, as crazy as it sounds, the library supports "partial application" for curry. You can define first arguments for delegates with up to 8 parameters:

Func<int, int, int, int, int, int, int, int, int> Multiply8 = (a, b, c, d, e, f, g, h) => a * b * c * d * e * f * g * h;
...
Func<int, int> curried = Multiply8.Curry(1, 2, 3, 4, 5, 6);
int result = curried(7)(8); // 40320

Uncurry

After curry you may want to get normal function back:

Func<int, Func<int, int>> multiply2curryed = Multiply.Curry(_, 2);
Func<int, int, int> multiply2 = multiply2curryed.Uncurry();
int result = multiply2(3, 4); // 24

Partial Application

The library has Partial Application extensions for all standard delegates:

Func<int, int, int> multiply2Partial = Multiply.Partial(2);
int result = multiply2Partial(3, 4); // 24

Also at the moment you can use the spacer for delegates with up to 6 parameters:

Func<int, int, int> multiply2Partial = Multiply.Partial(_, _, 2);
int result = multiply2Partial(3, 4); // 24

Pipe and Compose

You can chain functions with Pipe:

using static SCurry.Piper;
...
Func<int, int, int, int> Multiply = (a, b, c) => a * b * c;
Func<int, int> CurryedAdd2WithGap = TestFunctions.Add2.Curry(_, 2);
Func<int, Func<int, int>> CurryedPartialMultiplyBy3 = Multiply.Partial(_, _, 3).Curry();
Func<int, Func<int, Func<int, int>>> CurryedAdd3 = TestFunctions.Add3.Curry();

var piped = Pipe(
    CurryedAdd2WithGap,
    CurryedPartialMultiplyBy3(1),
    CurryedAdd3(3)(1)
);

var result = piped(0); // 10

Or Compose them:

using static SCurry.Composer;
...
var piped = Compose(
    CurryedAdd2WithGap,
    CurryedPartialMultiplyBy3(1),
    CurryedAdd3(3)(1)
);

var result = piped(0); // 14

Installation

PM> Install-Package SCurry
dotnet add package SCurry

Links

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.2.0 201 5/24/2020
1.1.1 198 5/23/2020
1.1.0 836 10/30/2018
1.0.0 225 10/29/2018