> 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/collection/fold.md).

# Fold

Applies a function to each element of the collection, threading an accumulator argument through the computation. Take the second argument, and apply the function to it and the first element of the collection. Then feed this result into the function along with the second element and so on.

This method is very similar to [`Reduce`](https://github.com/gabrielschade/tango/tree/379cc4a38ae47796971eb875ec66e7dc053a9081/Modules/Collection/reduce.html), but in this case is considered a initial `state`.

| Parameters                                                                             | Returns |
| -------------------------------------------------------------------------------------- | ------- |
| <p>Func\<TState, T, TState> folder</p><p>TState state</p><p>IEnumerable\<T> source</p> | TState  |

## Usage

&#x20;**Accumulating an individual property of each element through a collection**&#x20;

```csharp
//IEnumerable<Animal> source = 
//    { ("Cats",4), ("Dogs",5), ("Mice",3), ("Elephants",2) }

int result = source.Fold( 6, (_state, element) => _state + element.Item2);

//result = 20
```

When the type of elements in your collection are: `int`, `decimal`, `double`, `string` or `bool` you can also use this function combined with the `Operations` described in [operations section](https://github.com/gabrielschade/tango/tree/379cc4a38ae47796971eb875ec66e7dc053a9081/Operations/Introduction.html) as folder functions.

&#x20;**Using an operation as a folder**&#x20;

```csharp
//IEnumerable<int> source = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
int result = source.Fold(15, IntegerOperations.Add);

//result = 15 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
//result = 70
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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/modules/collection/fold.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.
