functional-c-sharp 1.1.0

dotnet add package functional-c-sharp --version 1.1.0
NuGet\Install-Package functional-c-sharp -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="functional-c-sharp" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add functional-c-sharp --version 1.1.0
#r "nuget: functional-c-sharp, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install functional-c-sharp as a Cake Addin
#addin nuget:?package=functional-c-sharp&version=1.1.0

// Install functional-c-sharp as a Cake Tool
#tool nuget:?package=functional-c-sharp&version=1.1.0

FunctionalCSharp

FunctionalCSharp is a library that makes it easier to write functional-oriented code in C#. It provides functional programming constructs such as partial application, pipe operator, and functional composition. So, give it a try and take your C# coding to the next level!

Working with FunctionalCSharp: An Overview

Fp

The Fp static class contains most of functional programming constructs of the library. Most of the methods are the extension methods of generic types or delegates.

Into

The Into extension method can be used to implement a functional-style pipeline, similar to the F# pipe operator. The method takes in an input value and returns an output value, allowing you to chain multiple method calls together in a fluent, readable manner.

Here's an example of how you might use the "Into" method to implement a simple calculation:

int result = 5.Into(x => x + 2)
              .Into(x => x * 3)
              .Into(x => x - 1);

The value 5 is passed through a pipeline of delegates (add 2, multiply by 3, subtract 1) to get the final result of 20.

Into1

The Into1 extension method is similar to the Into method, with the main difference being how the input value is passed to the first argument of the delegate.

int result = 5.Into1((x, y) => x + y, 2)
              .Into1((x, y) => x * y, 3)
              .Into1((x, y) => x - y, 1);

The value 5 is passed through a pipeline of delegates but as the first x argument (add 2, multiply by 3, subtract 1) to get the final result of 20.

Compose

The Compose extension method provides functionality for functional composition. The method allows you to combine multiple delegates into a single delegate, in a way that the output of one delegate becomes the input of the next delegate.

Here's an example of how you might use the "Compose" method to implement a simple calculation:

Func<int, int> add2 = x => x + 2;
Func<int, int> multiplyBy3 = x => x * 3;
Func<int, int> subtract1 = x => x - 1;

var composed = add2.Compose(multiplyBy3).Compose(subtract1);

int result = composed(5);

In this example, the add2, multiplyBy3, and subtract1 delegates are combined into a single delegate using the Compose method. The composed delegate takes an input of 5, adds 2 to it, multiplies the result by 3, and finally subtracts 1 to get a final result of 20.

ComposeBack

The ComposeBack extension method is similar to the Compose method, with the main difference being that delegates are composed backwards.

Func<int, int> add2 = x => x + 2;
Func<int, int> multiplyBy3 = x => x * 3;
Func<int, int> subtract1 = x => x - 1;

var composedBack = subtract1.ComposeBack(multiplyBy3).ComposeBack(add2);

int result = composedBack(5);
Partly

The Partly extension method provides functionality for partial application.

Func<int, int, int> add = (x, y) => x + y;

var partly = add.Partly(2);

Func<int, int> addTwo = partly;

int result = addTwo(3);

In this example, the add delegate takes two arguments and returns their sum. By using the Partly method and passing in the value 2, we create a new delegate addTwo that takes a single argument and adds 2 to it. When we call addTwo with an argument of 3, the result is 5.

Unit

The Unit extension method for the Fp class is similar to the Unit.Create method, but it immediately invokes the provided delegate or expression when called. See Unit.Create method

If

The If method is an extension method that allows you to create expression-like if statements in a functional style.

int input = 2;
int result = If(input > 3, 3)
                .Elif(input == 3, 2)
                .Else(1).Evaluate();

Console.WriteLine(result); //prints 1

When you omit the Evaluate method call, the result of the If/Elif/Else method will be an object that represents the expression-like if statement. This object can be stored and executed later.

Unit

The Unit class is a type that is used to represent an expression that has no value.

Func<int, Unit> print = x => {
    Console.WriteLine(x);
    return Unit.Value;
};

int input = 5;
print(input);

In this example, the print delegate takes an int argument and writes it to the console. The return type of the delegate is Unit, indicating that the delegate doesn't return a result. The delegate is used to perform a side effect, in this case printing to the console, and the return value of Unit.Value is ignored.

Create

The Create static method for the Unit class is used to create a delegate that wraps a provided delegate or expression, and returns Unit.Value. This method is used to transform expressions and statements that don't return a result into expressions that return a Unit value, allowing them to be used in a functional programming context.

Action<int> print = x => Console.WriteLine(x);
Func<int, Unit> printUnit = Unit.Create(print);

int input = 5;
printUnit(input);
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.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 Downloads Last updated
1.1.0 165 7/21/2023
1.0.0 182 2/12/2023