> For the complete documentation index, see [llms.txt](https://demystifyfp.gitbook.io/fstoolkit-errorhandling/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/fold.md).

# fold

Namespace: `FsToolkit.ErrorHandling`

## Function Signature

```fsharp
('a -> 'b) -> ('c -> 'b) 
    -> Result<'a, 'c> -> 'b
```

## Examples

### Example 1

`fold` can be used to convert `Result` to another similar type, such as `Choice`:

```fsharp
let choice1 = Ok 42 |> Result.fold Choice1Of2 Choice2Of2
// Choice1Of2 42

let choice2 = Error "An error occurred" |> Result.fold Choice1Of2 Choice2Of2
// Choice2Of2 "An error occurred"
```

### Example 2

In a typical web application, if there is any request validation error, we send `HTTP 400 Bad Request` as response and `HTTP 200 OK` for a successful operation.

Given the following function:

```fsharp
// string -> Result<int, string>
let tryParseInt str =
  match System.Int32.TryParse str with
  | true, x -> Ok x
  | false, _ -> 
    Error (sprintf "unable to parse '%s' to integer" str)
```

And the following fake HTTP response type:

```fsharp
type HttpResponse<'a, 'b> =
  | Ok of 'a
  | BadRequest of 'b
```

Then using `Result.fold`, we can do the following

```fsharp
// HttpRequest -> HttpResponse<int,string>
let handler httpRequest =
  // reading the input from the HTTP request
  let inputStr = httpRequest ... 
  inputStr |> tryParseInt |> Result.fold Ok BadRequest
```


---

# 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, and the optional `goal` query parameter:

```
GET https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/fold.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
