Skip to content

+ Missing Sequential for Choice #629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/FSharpPlus/Extensions/Extensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Choice<'T1, 'T2>>) =
let mutable error = ValueNone
Expand All @@ -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<Choice<'T, 'Choice2Of2>>) =
#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, 'Choice2Of2>>) : 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, 'Choice2Of2>>) : 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

Expand Down Expand Up @@ -438,4 +478,4 @@ module Extensions =
match t with
| ValueSome (Ok x) -> Ok (ValueSome x)
| ValueNone -> Ok ValueNone
| ValueSome (Error x) -> Error x
| ValueSome (Error x) -> Error x
Loading