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
  3. Sequences

traverseResultA

PrevioussequenceResultMNextsequenceResultA

Last updated 7 months ago

Namespace: FsToolkit.ErrorHandling

Function Signature

('a -> Result<'b,'c>) -> 'a seq -> Result<'b seq, 'c seq>

Note that traverse is the same as map >> sequence. See also .

This is applicative, collecting all errors. Compare the example below with .

See also Scott Wlaschin's .

Examples

Example 1

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

["1"; "2"; "3"]
|> Seq.traverseResultA tryParseInt
// Ok [1; 2; 3]

["1"; "foo"; "3"; "bar"]
|> Seq.traverseResultA tryParseInt
//  Error ["unable to parse 'foo' to integer";
//         "unable to parse 'bar' to integer"]

Example 2

// int -> Result<bool, string>
let isPrime (x : int) =
    if x < 2 then 
        sprintf "%i must be greater than 1" x |> Error
    elif 
        x = 2 then Ok true
    else
        let rec isPrime' (x : int) (i : int) =
            if i * i > x then Ok true
            elif x % i = 0 then Ok false
            else isPrime' x (i + 1)
        isPrime' x 2
  
// int seq -> Result<bool, string seq>      
let checkIfAllPrime (numbers : int seq) =
    numbers
    |> Seq.traverseResultA isPrime // Result<bool seq, string seq>
    |> Result.map (Seq.forall id) // shortened version of '|> Result.map (fun boolSeq -> boolSeq |> Seq.map (fun x -> x = true))'
    
let a = [1; 2; 3; 4; 5;] |> checkIfAllPrime
// Error ["1 must be greater than 1"]

let b = [1; 2; 3; 4; 5; 0;] |> checkIfAllPrime
// Error ["1 must be greater than 1"; "0 must be greater than 1"]

let a = [2; 3; 4; 5;] |> checkIfAllPrime
// Ok false

let a = [2; 3; 5;] |> checkIfAllPrime
// Ok true
Seq.sequenceResultA
traverseResultM
Understanding traverse and sequence