traverseJobResultM

List.traverseJobResultM

Namespace: FsToolkit.ErrorHandling

Function Signature:

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

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

This is monadic, stopping on the first error. Compare with traverseJobResultA, which collects all errors.

This is the same as traverseResultM except that it uses Job<Result<_,_>> instead of Result<_,_>.

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.traverseJobResultM tryParseIntJob
// job { return Ok [1; 2; 3] }

["1"; "foo"; "3"; "bar"]
|> List.traverseJobResultM tryParseIntJob
// job { return Error "unable to parse 'foo' to integer" }
// stops at first error

Example 2

Last updated