FsToolkit.ErrorHandling
  • README
  • FsToolkit.ErrorHandling
    • Result
      • apply
      • bind
      • check
      • Computation Expression
      • either Functions
      • fold
      • ignore
      • map
      • map2
      • map3
      • mapError
      • Operators
      • orElse Functions
      • Other Functions
      • require Functions
      • tee Functions
      • tryCreate
      • zip
      • zipError
      • Lists
        • traverseResultM
        • sequenceResultM
        • traverseResultA
        • sequenceResultA
      • Sequences
        • traverseResultM
        • sequenceResultM
        • traverseResultA
        • sequenceResultA
      • Transforms
        • ofChoice
    • Option
      • bind
      • bindNull
      • Computation Expression
      • either
      • map
      • map2
      • map3
      • sequenceAsync
      • sequenceResult
      • sequenceTask
      • sequenceTaskResult
      • tee Functions
      • traverseAsync
      • traverseResult
      • traverseTask
      • traverseTaskResult
      • zip
      • Lists
        • traverseOptionM
        • sequenceOptionM
        • traverseVOptionM
        • sequenceVOptionM
      • Sequences
        • traverseOptionM
        • sequenceOptionM
        • traverseVOptionM
        • sequenceVOptionM
      • Transforms
        • ofNull
        • ofPair
        • ofResult
        • ofValueOption
        • toValueOption
    • ResultOption
      • apply
      • bind
      • Computation Expression
      • ignore
      • map
      • map2
      • map3
      • mapError
      • Operators
      • zip
      • zipError
      • Transforms
        • ofChoice
        • ofOption
        • ofResult
    • AsyncResult
      • apply
      • bind
      • check
      • Computation Expression
      • error
      • foldResult
      • ignore
      • map
      • map2
      • map3
      • mapError
      • Operators
      • Other Functions
      • zip
      • zipError
      • List
        • traverseAsyncResultM
        • sequenceAsyncResultM
        • traverseAsyncResultA
        • sequenceAsyncResultA
      • Sequences
        • traverseAsyncResultM
        • sequenceAsyncResultM
        • traverseAsyncResultA
        • sequenceAsyncResultA
      • Transforms
        • ofAsync
        • ofResult
        • ofTask
        • ofTaskAction
    • AsyncOption
      • apply
      • bind
      • Computation Expression
      • either
      • map
      • orElse Functions
      • Other Functions
    • AsyncResultOption
      • apply
      • bind
      • Computation Expression
      • ignore
      • map
      • map2
      • map3
      • Operators
      • Transforms
        • ofAsyncOption
        • ofAsyncResult
        • ofOption
        • ofResult
    • Task
      • apply
      • bind
      • bindV
      • catch
      • Computation Expression
      • ignore
      • map
      • mapV
      • map2
      • map3
      • zip
      • Transforms
        • ofUnit
    • TaskOption
      • apply
      • bind
      • Computation Expression
      • either
      • map
      • Other Functions
      • zip
    • TaskResult
      • apply
      • bind
      • check
      • catch
      • Computation Expression
      • ignore
      • map
      • map2
      • map3
      • mapError
      • Operators
      • Other Functions
      • error
      • zip
      • zipError
      • Lists
        • traverseTaskResultM
        • sequenceTaskResultM
        • traverseTaskResultA
        • sequenceTaskResultA
      • Transforms
        • ofAsync
        • ofResult
        • ofTask
    • TaskResultOption
      • apply
      • bind
      • Computation Expression
      • ignore
      • map
      • map2
      • map3
      • Operators
    • Validation
      • apply
      • Computation Expression
      • error
      • map
      • map2
      • map3
      • mapError
      • mapErrors
      • Operators
      • zip
      • Transforms
        • ofChoice
        • ofResult
    • AsyncValidation
      • apply
      • Computation Expression
      • error
      • map
      • map2
      • map3
      • mapError
      • mapErrors
      • Operators
      • zip
      • Transforms
        • ofChoice
        • ofResult
    • TaskValidation
      • apply
      • Computation Expression
      • error
      • map
      • map2
      • map3
      • mapError
      • mapErrors
      • Operators
      • zip
      • Transforms
        • ofChoice
        • ofResult
  • FsToolkit.ErrorHandling.AsyncSeq
    • AsyncSeq
      • Computation Expression
  • FsToolkit.ErrorHandling.IcedTasks
    • CancellableTaskResult
      • apply
      • bind
      • Computation Expression
      • getCancellationToken
      • map
      • Operators
      • Other Functions
      • zip
      • parallelZip
    • CancellableTaskValidation
      • apply
      • bind
      • Computation Expression
      • error
      • getCancellationToken
      • map
      • map2
      • map3
      • mapError
      • mapErrors
      • Operators
      • zip
      • parallelZip
      • Transforms
        • ofChoice
        • ofResult
  • FsToolkit.ErrorHandling.JobResult
    • Job
      • apply
      • map2
      • map3
      • singleton
      • zip
    • JobOption
      • apply
      • bind
      • ce
      • either
      • map
    • JobResult
      • apply
      • bind
      • catch
      • Computation Expression
      • error
      • fromTask
      • fromUnitTask
      • ignore
      • map
      • map2
      • map3
      • mapError
      • Operators
      • Other Functions
      • zip
      • zipError
      • Lists
        • traverseJobResultM
        • sequenceJobResultM
        • traverseJobResultA
        • sequenceJobResultA
      • Transforms
        • ofAsync
        • ofJob
        • ofResult
        • fromTask
        • fromUnitTask
    • JobResultOption
      • apply
      • bind
      • Computation Expression
      • ignore
      • map
      • map2
      • map3
  • General Docs
    • Bind Mappings
Powered by GitBook
On this page
  • Function Signature
  • Examples
  • Example 1
  • Example 2
  1. FsToolkit.ErrorHandling
  2. Result

map3

Previousmap2NextmapError

Last updated 10 months ago

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 .

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:

let okResult =
  Result.map3 add (tryParseInt "35") (tryParseInt "5") (tryParseInt "2")
  // Ok 42

let errorResult =
  Result.map3 add (tryParseInt "40") (tryParseInt "foobar") (tryParseInt "2")
  // Error "unable to parse 'foobar' to integer"

Example 2

UserId

type UserId = UserId of Guid

Tweet

type Tweet = private Tweet of string with
  member this.Value = let (Tweet tweet) = this in tweet

  static member TryCreate (tweet : string) =
    if String.IsNullOrEmpty(tweet) then
      Error "Tweet shouldn't be empty"
    elif tweet.Length > 280 then
      Error "Tweet shouldn't contain more than 280 characters"
    else Ok (Tweet tweet)

CreatePostRequest

type CreatePostRequest =
  { UserId : UserId
    Tweet : Tweet
    Location : Location option }
  static member Create userId lat long tweet =
    { Tweet = tweet
      Location = Some (location lat long)
      UserId = userId }

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

let validLatR = Latitude.TryCreate 13.067439
let validLngR = Longitude.TryCreate 80.237617
let validTweetR = Tweet.TryCreate "Hello, World!"
let userId  = UserId (Guid.NewGuid())

let result =
  Result.map3 (CreatePostRequest.Create userId) validLatR validLngR validTweetR
(* Ok {UserId = UserId 6c0fb13f-ff91-46c7-a486-201257f18877;
       Tweet = Tweet "Hello, World!";
       Location = Some {Latitude = Latitude 13.067439;
                        Longitude = Longitude 80.237617;};} *)

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

let invalidLatR = Latitude.TryCreate 200.
let result =
  Result.map3 (CreatePostRequest.Create userId) invalidLatR validLngR validTweetR
  // Error "200.0 is a invalid latitude value"

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

the result computation expression
map2