From 9ebbe7f47dd338867cfdfcbc201bb474be9c9194 Mon Sep 17 00:00:00 2001 From: Gus <1261319+gusty@users.noreply.github.com> Date: Sun, 8 Jun 2025 10:58:20 +0200 Subject: [PATCH 1/3] + Sequential for Choice with (v)options --- src/FSharpPlus/Extensions/Extensions.fs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/FSharpPlus/Extensions/Extensions.fs b/src/FSharpPlus/Extensions/Extensions.fs index 4bdfbb37f..7cf2e2ca3 100644 --- a/src/FSharpPlus/Extensions/Extensions.fs +++ b/src/FSharpPlus/Extensions/Extensions.fs @@ -336,6 +336,20 @@ module Extensions = | 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 + /// Returns all Choice2Of2's combined, otherwise a sequence of all Choice1Of2 elements. static member Parallel (choice2Combiner, t: seq>) = @@ -438,4 +452,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 From cf27f9531705a4a6ffa5d61a5800548723a9bead Mon Sep 17 00:00:00 2001 From: gusty <1261319+gusty@users.noreply.github.com> Date: Sat, 14 Jun 2025 07:23:35 +0200 Subject: [PATCH 2/3] Move code down --- src/FSharpPlus/Extensions/Extensions.fs | 29 ++++++++++++------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/FSharpPlus/Extensions/Extensions.fs b/src/FSharpPlus/Extensions/Extensions.fs index 7cf2e2ca3..0a12facce 100644 --- a/src/FSharpPlus/Extensions/Extensions.fs +++ b/src/FSharpPlus/Extensions/Extensions.fs @@ -336,21 +336,6 @@ module Extensions = | 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 - - /// Returns all Choice2Of2's combined, otherwise a sequence of all Choice1Of2 elements. static member Parallel (choice2Combiner, t: seq>) = let mutable error = ValueNone @@ -367,6 +352,20 @@ module Extensions = | ValueNone -> Choice1Of2 (Array.toSeq res) | ValueSome e -> Choice2Of2 e + /// 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 From 373ef6f3cc3b70380820dc7bdb3f32d46eacccbd Mon Sep 17 00:00:00 2001 From: gusty <1261319+gusty@users.noreply.github.com> Date: Sat, 14 Jun 2025 07:25:08 +0200 Subject: [PATCH 3/3] + Sequential for list of Choice --- src/FSharpPlus/Extensions/Extensions.fs | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/FSharpPlus/Extensions/Extensions.fs b/src/FSharpPlus/Extensions/Extensions.fs index 0a12facce..c9e42f3ec 100644 --- a/src/FSharpPlus/Extensions/Extensions.fs +++ b/src/FSharpPlus/Extensions/Extensions.fs @@ -352,6 +352,33 @@ 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