> For the complete documentation index, see [llms.txt](https://gabriel-schade-cardoso.gitbook.io/tango/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gabriel-schade-cardoso.gitbook.io/tango/modules/option/fold.md).

# Fold

Creates a new `TState` value by applying the given `folder` function to `state` and `Option<T>.Some` option value. Otherwise returns the `state` itself.

| Parameters                                                                        | Returns |
| --------------------------------------------------------------------------------- | ------- |
| <p>Func\<TState, T, TState> folder</p><p>TState state</p><p>Option\<T> option</p> | TState  |

## Usage

This function applies the `folder` function to the `Option<T>` value and to the `state`.

When the optional value `IsNone` the `folder` function won't be executed and the `state` is returned as a result.

&#x20;**When the option value IsSome**&#x20;

```csharp
string state = "The number is: "
Option<int> optionValue = 10;
string result = optionValue.Fold(
                    state,
                    (_state, value) => string.Concat(_state, value) );

//result = "The number is: 10"
```

&#x20;**When the option value IsNone**&#x20;

```csharp
string state = "The number is: "
Option<int> optionValue = Option<int>.None();
string result = optionValue.Fold(
                    state,
                    (_state, value) => string.Concat(_state, value) );

//result = "The number is: "
```

&#x20;**When the option value IsSome**&#x20;

```csharp
int state = 30
Option<int> optionValue = 10;
int result = optionValue.Fold(
                 state,
                 (_state, value) => _state + value );

//result = 40
```

&#x20;**When the option value IsNone**

```csharp
int state = 30
Option<int> optionValue = Option<int>.None();
int result = optionValue.Fold(
                 state,
                 (_state, value) => _state + value );

//result = 30
```
