diff --git a/src/FSharpPlus/Extensions/Extensions.fs b/src/FSharpPlus/Extensions/Extensions.fs index 4bdfbb37f..c9e42f3ec 100644 --- a/src/FSharpPlus/Extensions/Extensions.fs +++ b/src/FSharpPlus/Extensions/Extensions.fs @@ -336,7 +336,6 @@ module Extensions = | ValueSome x -> Choice2Of2 x #endif - /// Returns all Choice2Of2's combined, otherwise a sequence of all Choice1Of2 elements. static member Parallel (choice2Combiner, t: seq>) = let mutable error = ValueNone @@ -353,6 +352,47 @@ module Extensions = | ValueNone -> Choice1Of2 (Array.toSeq res) | ValueSome e -> Choice2Of2 e + /// Returns the first Choice2Of2 if it contains a Choice2Of2 element, otherwise a list of all elements. + static member Sequential (t: list>) = + #if FABLE_COMPILER + let mutable error = ValueNone + let res = Seq.toList (seq { + use e = (t :> seq<_>).GetEnumerator () + while e.MoveNext () && error.IsNone do + match e.Current with + | Choice1Of2 v -> yield v + | Choice2Of2 e -> error <- ValueSome e }) + + match error with + | ValueNone -> Choice1Of2 res + | ValueSome x -> Choice2Of2 x + #else + let mutable accumulator = ListCollector<'T> () + let mutable error = ValueNone + use e = (t :> seq<_>).GetEnumerator () + while e.MoveNext () && error.IsNone do + match e.Current with + | Choice1Of2 v -> accumulator.Add v + | Choice2Of2 x -> error <- ValueSome x + match error with + | ValueNone -> Choice1Of2 (accumulator.Close ()) + | ValueSome x -> Choice2Of2 x + #endif + + /// Returns the Choice2Of2 if it contains an Choice2Of2 element, otherwise the option inside a Choice1Of2. + static member Sequential (t: option>) : Choice<'T option, 'Choice2Of2> = + match t with + | Some (Choice1Of2 x) -> Choice1Of2 (Some x) + | Some (Choice2Of2 x) -> Choice2Of2 x + | None -> Choice1Of2 None + + /// Returns the Choice2Of2 if it contains an Choice2Of2 element, otherwise the option inside a Choice1Of2. + static member Sequential (t: voption>) : Choice<'T voption, 'Choice2Of2> = + match t with + | ValueSome (Choice1Of2 x) -> Choice1Of2 (ValueSome x) + | ValueSome (Choice2Of2 x) -> Choice2Of2 x + | ValueNone -> Choice1Of2 ValueNone + type Result<'T, 'Error> with @@ -438,4 +478,4 @@ module Extensions = match t with | ValueSome (Ok x) -> Ok (ValueSome x) | ValueNone -> Ok ValueNone - | ValueSome (Error x) -> Error x \ No newline at end of file + | ValueSome (Error x) -> Error x