Functional Extensions

Tango.Functional.FunctionExtensions

This static class contains several overloads to deal with Action and Func delegates as extension methods. With this class is possible to use Currying and PartialApplication classes as extension methods as well.

Besides that, this class provide an conversion mechanism to cast an Action into a Func that returns an Unit type and vice versa.

Methods

Name

Parameters

Returns

Description

ToFunction

this Action action

Func<Unit>

Casts an action to a function that returns a new instance of Unit.

ToFunction

this Action<T> action

Func<T, Unit>

Casts an action to a function that returns a new instance of Unit.

ToFunction

this Action<T, T2> action

Func<T, T2, Unit>

Converte um delegate Action para um delegate Func, mantendo os mesmos parâmetros e retornando um Unit.

ToFunction

this Action<T, T2, T3> action

Func<T, T2, T3, Unit>

Casts an action to a function that returns a new instance of Unit.

ToFunction

this Action<T, T2, T3, T4> action

Func<T, T2, T3, T4, Unit>

Casts an action to a function that returns a new instance of Unit.

ToAction

this Func<Unit> function

Action

Casts a function that returns Unit to an action.

ToAction

this Func<T, Unit> function

Action<T>

Casts a function that returns Unit to an action.

ToAction

this Func<T, T2, Unit> function

Action<T, T2>

Casts a function that returns Unit to an action.

ToAction

this Func<T, T2, T3, Unit> function

Action<T, T2, T3>

Casts a function that returns Unit to an action.

ToAction

this Func<T, T2, T3, T4, Unit> function

Action<T, T2, T3, T4>

Casts a function that returns Unit to an action.

WARNING

This class contains more than the listed methods, however, all of the non listed methods are just overloads for methods avaible in Currying and PartialApplication as an extension method.

Usage

Different from another classes of this namespace, this class can act as an extension to Func and Action C# delegates.

After using the namespace Tango.Functional you'll be capable of doing all these operations like they are methods of the delegates itselves!

See the code bellow:

Action<int> writeNumber = (number) => Console.WriteLine(number);
Func<int, Unit> functionWriteNumber = writeNumber.ToFunction();

It is a casts from one delegate to another in an extremely easy way!

It can help you to uses high order functions when the necessary parameter is from one specific type of delegate!

The Curry and Partial Application operations works very similar to the described in sections: Currying and PartialApplication, however, through this class, the methods can be used as an extension.

 Func<int, int, int> add = (value, value2) => value + value2;

Podemos realizar tanto o processo de currying quanto de aplicação parcial, conforme código:

 Func<int, int, int> add = 
   (value, value2) => value + value2;

Func<int, Func<int,int>> addCurried = add.Curry();
Func<int,int> addPartialApplied = add.PartialApply(2);

The currying and the partial application works just fine, like methods from the delegate itself.

add.Curry();
//instead of
Currying.Curry(add);


add.PartialApply(2);
//instead of
PartialApplication.PartialApply(add,2);

The fundamentals about Currying and Partial Application can be find in Fundamentals > Currying and Partial Application.

Last updated