Skip to content

Commit

Permalink
+ Result.Sequence (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty authored Nov 8, 2023
1 parent 0dfde01 commit 1970fea
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/FSharpPlus/Extensions/Extensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ module Extensions =
| Some v -> yield v
| None -> ok <- false })
if ok then Some (Array.toSeq res) else None

type ValueOption<'t> with

/// Returns None if it contains a None element, otherwise a list of all elements
Expand All @@ -146,4 +147,19 @@ module Extensions =
match e.Current with
| ValueSome v -> yield v
| ValueNone -> ok <- false })
if ok then ValueSome (Array.toSeq res) else ValueNone
if ok then ValueSome (Array.toSeq res) else ValueNone

type Result<'t, 'error> with

/// Returns the first Error if it contains an Error element, otherwise a list of all elements
static member Sequence (t: seq<Result<'T, ' Error>>) =
let mutable bad = None
let res = Seq.toArray (seq {
use e = t.GetEnumerator ()
while e.MoveNext () && bad.IsNone do
match e.Current with
| Ok v -> yield v
| Error x -> bad <- Some x })
match bad with
| None-> Ok (Array.toSeq res)
| Some x -> Error x

0 comments on commit 1970fea

Please sign in to comment.