RSCG – PartiallyApplied

RSCG – PartiallyApplied
 
 

name PartiallyApplied
nuget https://www.nuget.org/packages/PartiallyApplied/
link https://github.com/JasonBock/PartiallyApplied/blob/main/docs/Quickstart.md
author Jason Bock

If you need to curry functions, you can use this package

 

This is how you can use PartiallyApplied .

The code that you start with is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
<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>

The code that you will use is

01
02
03
04
05
06
07
08
09
10
11
12
13
using System;
 
namespace PartFunc;
class Program
{
    static void Main(string[] args)
    {
         
        var disc10Percent = Partially.Apply(Accounting.Discount, 1/10f);
        Console.WriteLine(disc10Percent(disc10Percent(100)));
         
    }
}
01
02
03
04
05
06
07
08
09
10
namespace PartFunc;
 
public class Accounting
{
    public static float Discount( float discount, float price)
    {
        var val= price * (1- discount);
        return val;
    }
}

 

The code that is generated is

1
2
3
4
5
6
7
8
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));
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/PartiallyApplied