sequenceResultM

Namespace: FsToolkit.ErrorHandling

Function Signature

seq<Result<'a, 'b>> -> Result<'a[], 'b>

This is monadic, stopping on the first error. Compare the example below with sequenceResultA.

See also Scott Wlaschin's Understanding traverse and sequence.

Examples

Example 1

// string -> Result<int, string>
let tryParseInt str =
  match Int32.TryParse str with
  | true, x -> Ok x
  | false, _ -> Error $"unable to parse '{str}' to integer"

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

seq { "1"; "foo"; "3"; "bar" }
|> Seq.map tryParseInt
|> Seq.sequenceResultM  
// Error "unable to parse 'foo' to integer"

Example 2

Last updated