map3

Namespace: FsToolkit.ErrorHandling

Function Signature

('a -> 'b -> 'c -> 'd) -> Result<'a, 'e> -> Result<'b, 'e>
    -> Result<'c, 'e> -> Result<'d, 'e>

Examples

Note: Many use-cases requiring map operations can also be solved using the result computation expression.

Example 1

Let's assume that we have an add function that adds three numbers:

// int -> int -> int -> int
let add a b c = a + b + c

And an another function that converts a string to an integer:

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

With the help of Result.map3 function, we can now do the following:

Example 2

Let's assume that we have the following types in addition to the types that we saw in the map2 example to model a request for posting a tweet:

UserId

Tweet

CreatePostRequest

Then, we can use the Result.map3 function as below to create the CreatePostRequest with validation:

When we try with an invalid latitude value, we'll get the following result:

Last updated