Skip to content

Commit 709558e

Browse files
gustywallymathieu
authored andcommitted
+ Missing Sequential for Choice (#629)
1 parent 3d80a3a commit 709558e

1 file changed

Lines changed: 42 additions & 2 deletions

File tree

src/FSharpPlus/Extensions/Extensions.fs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ module Extensions =
350350
#endif
351351
[<Obsolete("Renamed to Choice.Sequential")>]static member Sequence (t: seq<Choice<_, _>>) = Choice<_, _>.Sequential t
352352

353-
354353
/// Returns all Choice2Of2's combined, otherwise a sequence of all Choice1Of2 elements.
355354
static member Parallel (choice2Combiner, t: seq<Choice<'T1, 'T2>>) =
356355
let mutable error = ValueNone
@@ -367,6 +366,47 @@ module Extensions =
367366
| ValueNone -> Choice1Of2 (Array.toSeq res)
368367
| ValueSome e -> Choice2Of2 e
369368

369+
/// Returns the first Choice2Of2 if it contains a Choice2Of2 element, otherwise a list of all elements.
370+
static member Sequential (t: list<Choice<'T, 'Choice2Of2>>) =
371+
#if FABLE_COMPILER
372+
let mutable error = ValueNone
373+
let res = Seq.toList (seq {
374+
use e = (t :> seq<_>).GetEnumerator ()
375+
while e.MoveNext () && error.IsNone do
376+
match e.Current with
377+
| Choice1Of2 v -> yield v
378+
| Choice2Of2 e -> error <- ValueSome e })
379+
380+
match error with
381+
| ValueNone -> Choice1Of2 res
382+
| ValueSome x -> Choice2Of2 x
383+
#else
384+
let mutable accumulator = ListCollector<'T> ()
385+
let mutable error = ValueNone
386+
use e = (t :> seq<_>).GetEnumerator ()
387+
while e.MoveNext () && error.IsNone do
388+
match e.Current with
389+
| Choice1Of2 v -> accumulator.Add v
390+
| Choice2Of2 x -> error <- ValueSome x
391+
match error with
392+
| ValueNone -> Choice1Of2 (accumulator.Close ())
393+
| ValueSome x -> Choice2Of2 x
394+
#endif
395+
396+
/// Returns the Choice2Of2 if it contains an Choice2Of2 element, otherwise the option inside a Choice1Of2.
397+
static member Sequential (t: option<Choice<'T, 'Choice2Of2>>) : Choice<'T option, 'Choice2Of2> =
398+
match t with
399+
| Some (Choice1Of2 x) -> Choice1Of2 (Some x)
400+
| Some (Choice2Of2 x) -> Choice2Of2 x
401+
| None -> Choice1Of2 None
402+
403+
/// Returns the Choice2Of2 if it contains an Choice2Of2 element, otherwise the option inside a Choice1Of2.
404+
static member Sequential (t: voption<Choice<'T, 'Choice2Of2>>) : Choice<'T voption, 'Choice2Of2> =
405+
match t with
406+
| ValueSome (Choice1Of2 x) -> Choice1Of2 (ValueSome x)
407+
| ValueSome (Choice2Of2 x) -> Choice2Of2 x
408+
| ValueNone -> Choice1Of2 ValueNone
409+
370410

371411
type Result<'T, 'Error> with
372412

@@ -453,4 +493,4 @@ module Extensions =
453493
match t with
454494
| ValueSome (Ok x) -> Ok (ValueSome x)
455495
| ValueNone -> Ok ValueNone
456-
| ValueSome (Error x) -> Error x
496+
| ValueSome (Error x) -> Error x

0 commit comments

Comments
 (0)