PartiallyApplied by Jason Bock
Nuget / site data
Details
Info
Name: PartiallyApplied
A way to do partial function application in C#
Author: Jason Bock
NuGet: https://www.nuget.org/packages/PartiallyApplied/
You can find more details at https://github.com/JasonBock/PartiallyApplied/blob/main/docs/Quickstart.md
Original Readme
PartiallyApplied
A way to do partial function application in C#.
Overview
You can find this code as a package in NuGet. Once installed, you can use it to do partial function application:
public static class Maths
{
public static int Add(int a, int b) => a + b;
}
public static class Runner
{
public static void Run()
{
var incrementBy3 = Partially.Apply(Maths.Add, 3);
var value = incrementBy3(4);
// value is now equal to 7.
}
}
More details can be found on the Quickstart page. Note that if you build the code locally, you'll need to build in Release
mode for the package reference in PartiallyApplied.NuGetHost
to resolve correctly (or unload that project from the solution as it's optional and delete nuget.config
).
About
If you need to curry functions, you can use this package
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Accounting.cs
This is the CSharp Project that references PartiallyApplied
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PartiallyApplied" Version="1.3.0" />
</ItemGroup>
</Project>
This is the use of PartiallyApplied in Program.cs
using System;
namespace PartFunc;
class Program
{
static void Main(string[] args)
{
var disc10Percent = Partially.Apply(Accounting.Discount, 1/10f);
Console.WriteLine(disc10Percent(disc10Percent(100)));
}
}
This is the use of PartiallyApplied in Accounting.cs
namespace PartFunc;
public class Accounting
{
public static float Discount( float discount, float price)
{
var val= price * (1- discount);
return val;
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- Partially.g.cs
using System;
#nullable enable
public static partial class Partially
{
public static Func<float, float> Apply(Func<float, float, float> method, float discount) =>
new((price) => method(discount, price));
}
Usefull
Download Example (.NET C# )
Share PartiallyApplied
https://ignatandrei.github.io/RSCG_Examples/v2/docs/PartiallyApplied