# Functional Extensions

> [`Tango.Functional.FunctionExtensions`](https://github.com/gabrielschade/Tango/blob/master/Tango/Tango/Functional/FunctionExtensions.cs)

This *static* class contains several overloads to deal with `Action` and `Func` delegates as extension methods. With this class is possible to use [`Currying`](https://github.com/gabrielschade/tango/tree/379cc4a38ae47796971eb875ec66e7dc053a9081/Functional/Currying.html) and [`PartialApplication`](https://github.com/gabrielschade/tango/tree/379cc4a38ae47796971eb875ec66e7dc053a9081/Functional/PartialApplication.html) 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.                                                       |

> &#x20;**WARNING**&#x20;
>
> This class contains more than the listed methods, however, all of the non listed methods are just overloads for methods avaible in [`Currying`](https://github.com/gabrielschade/tango/tree/379cc4a38ae47796971eb875ec66e7dc053a9081/Functional/Currying.html) and [`PartialApplication`](https://github.com/gabrielschade/tango/tree/379cc4a38ae47796971eb875ec66e7dc053a9081/Functional/PartialApplication.html) 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:

```csharp
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`](https://github.com/gabrielschade/tango/tree/379cc4a38ae47796971eb875ec66e7dc053a9081/Functional/Currying.html) and [`PartialApplication`](https://github.com/gabrielschade/tango/tree/379cc4a38ae47796971eb875ec66e7dc053a9081/Functional/PartialApplication.html), however, through this class, the methods can be used as an extension.

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

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

```csharp
 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.

```csharp
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](https://github.com/gabrielschade/tango/tree/379cc4a38ae47796971eb875ec66e7dc053a9081/Concepts/Currying%20and%20Partial%20Application.html).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gabriel-schade-cardoso.gitbook.io/tango/functional/functionextensions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
