traverseResultM
Namespace: FsToolkit.ErrorHandling
Function Signature
('a -> Result<'b,'c>) -> 'a seq -> Result<'b seq, 'c>Note that traverse is the same as map >> sequence. See also Seq.sequenceResultM.
This is monadic, stopping on the first error. Compare the example below with traverseResultA.
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 (sprintf "unable to parse '%s' to integer" str)
["1"; "2"; "3"]
|> Seq.traverseResultM tryParseInt
// Ok [1; 2; 3]
["1"; "foo"; "3"; "bar"]
|> Seq.traverseResultM tryParseInt
// Error "unable to parse 'foo' to integer"Example 2
Last updated