traverseJobResultA

List.traverseJobResultA

Namespace: FsToolkit.ErrorHandling

Function Signature:

('a -> Job<Result<'b, 'c>>) -> 'a list -> Job<Result<'b list, 'c list>>

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

This is applicative, collecting all errors rather than stopping at the first. Compare with traverseJobResultM, which short-circuits on first error.

See also Scott Wlaschin's Understanding traverse and sequencearrow-up-right.

Examples

Example 1

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

["1"; "2"; "3"]
|> List.traverseJobResultA tryParseIntJob
// job { return Ok [1; 2; 3] }

["1"; "foo"; "3"; "bar"]
|> List.traverseJobResultA tryParseIntJob
// job { return Error ["unable to parse 'foo' to integer"; "unable to parse 'bar' to integer"] }
// collects all errors

Example 2

Last updated