> 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/resultoption/map2.md).

# map2

Namespace: `FsToolkit.ErrorHandling`

Function Signature:

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

## Examples

### Example 1

Given the following function:

```fsharp
let add : int -> int -> int
```

Then using `ResultOption.map2`, we can do the following:

```fsharp
ResultOption.map2 add (Ok (Some 40)) (Ok (Some 2)) 
// Ok (Some 42)
```

### Example 2

Let's assume that we have the following types and functions in addition to what we defined in the [Result.map2 example](/fstoolkit-errorhandling/fstoolkit.errorhandling/result/map2.md#example-2):

```fsharp
type CreatePostRequest = {
  Tweet : Tweet
  Location : Location option
}

// Tweet -> Location option -> CreatePostRequest
let createPostRequest tweet location =
  {Tweet = tweet; Location = location}

type CreatePostRequestDto = {
  Tweet : string
  Latitude : float option
  Longitude : float option
}
```

We can then create a function transforming a `CreatePostRequestDto` to a `CreatePostRequest`, using `Option.traverseResult`, `ResultOption.map2`, and `Result.map2`:

```fsharp
// CreatePostRequestDto -> Result<CreatePostRequest, string>
let toCreatePostRequest (dto : CreatePostRequestDto) = 

  // Result<Latitude option, string>
  let latR =
    dto.Latitude
    |> Option.traverseResult Latitude.TryCreate

  // Result<Longitude option, string>
  let lngR =
    dto.Longitude
    |> Option.traverseResult Longitude.TryCreate

  // Result<Location option, string>
  let locationR =
    ResultOption.map2 location.Create latR lngR

  // Result<Tweet, string>
  let tweetR = Tweet.TryCreate dto.Tweet

  // Result<CreatePostRequest, string>
  Result.map2 createPostRequest tweetR locationR
```

Note that this example can also be written using the `result` and `resultOption` computation expressions, which would allow you to skip the `map2` functions. See for example the [resultOption CE example](/fstoolkit-errorhandling/fstoolkit.errorhandling/result/ce.md#example-2).


---

# 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/resultoption/map2.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.
