# traverseResultA

Namespace: `FsToolkit.ErrorHandling`

## Function Signature

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

Note that `traverse` is the same as `map >> sequence`. See also [Seq.sequenceResultA](/fstoolkit-errorhandling/fstoolkit.errorhandling/result/sequences/sequenceresulta.md).

This is applicative, collecting all errors. Compare the example below with [traverseResultM](/fstoolkit-errorhandling/fstoolkit.errorhandling/result/sequences/traverseresultm.md).

See also Scott Wlaschin's [Understanding traverse and sequence](https://fsharpforfunandprofit.com/posts/elevated-world-4/).

## Examples

### Example 1

```fsharp
// 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

```fsharp
// 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
```


---

# 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/sequences/traverseresulta.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.
