# apply

Namespace: `FsToolkit.ErrorHandling`

`apply` combines two Result values and returns a new Result value. If both Result values are Ok, it applies the function from the first Result to the value from the second Result, producing a new Result type. If either Result is an Error, the apply function propagates the error by returning the corresponding Error value.

## Function Signature

```fsharp
Result<('okInput -> 'okOutput), 'error> -> Result<'okInput, 'error> 
    -> Result<'okOutput, 'error>
```

## Examples

### Example 1

Note: The `apply` function is most useful in its infix operator form `<*>` when using it over a multi-parameter function. Examples of this is shown in [the documentation for this operator](/fstoolkit-errorhandling/fstoolkit.errorhandling/result/operators.md). The example below is a bit artificial since `map` is arguably better.

Assume that we have a function to find the remaining characters of a [Tweet](/fstoolkit-errorhandling/fstoolkit.errorhandling/result/map3.md#tweet):

```fsharp
// Tweet -> int
let remainingCharacters (tweet : Tweet) =
  280 - tweet.Value.Length
```

If we want a function that takes a plain string instead, we can achieve it using the `apply` function:

```fsharp
// string -> Result<int,string>
let remainingCharactersStr (tweetStr : string) =
  Tweet.TryCreate tweet
  |> Result.apply (Ok remainingCharacters)
```

But as mentioned, using the `map` function is simpler in this case:

```fsharp
// string -> Result<int,string>
let remainingCharactersStr (tweetStr : string) =
  Tweet.TryCreate tweet
  |> Result.map remainingCharacters
```


---

# 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://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/result/apply.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.
