Skip to content

Commit 09af101

Browse files
authored
+ Missing Sequential for Choice (#629)
1 parent 007b8cc commit 09af101

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/FSharpPlus/Extensions/Extensions.fs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ module Extensions =
336336
| ValueSome x -> Choice2Of2 x
337337
#endif
338338

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

355+
/// Returns the first Choice2Of2 if it contains a Choice2Of2 element, otherwise a list of all elements.
356+
static member Sequential (t: list<Choice<'T, 'Choice2Of2>>) =
357+
#if FABLE_COMPILER
358+
let mutable error = ValueNone
359+
let res = Seq.toList (seq {
360+
use e = (t :> seq<_>).GetEnumerator ()
361+
while e.MoveNext () && error.IsNone do
362+
match e.Current with
363+
| Choice1Of2 v -> yield v
364+
| Choice2Of2 e -> error <- ValueSome e })
365+
366+
match error with
367+
| ValueNone -> Choice1Of2 res
368+
| ValueSome x -> Choice2Of2 x
369+
#else
370+
let mutable accumulator = ListCollector<'T> ()
371+
let mutable error = ValueNone
372+
use e = (t :> seq<_>).GetEnumerator ()
373+
while e.MoveNext () && error.IsNone do
374+
match e.Current with
375+
| Choice1Of2 v -> accumulator.Add v
376+
| Choice2Of2 x -> error <- ValueSome x
377+
match error with
378+
| ValueNone -> Choice1Of2 (accumulator.Close ())
379+
| ValueSome x -> Choice2Of2 x
380+
#endif
381+
382+
/// Returns the Choice2Of2 if it contains an Choice2Of2 element, otherwise the option inside a Choice1Of2.
383+
static member Sequential (t: option<Choice<'T, 'Choice2Of2>>) : Choice<'T option, 'Choice2Of2> =
384+
match t with
385+
| Some (Choice1Of2 x) -> Choice1Of2 (Some x)
386+
| Some (Choice2Of2 x) -> Choice2Of2 x
387+
| None -> Choice1Of2 None
388+
389+
/// Returns the Choice2Of2 if it contains an Choice2Of2 element, otherwise the option inside a Choice1Of2.
390+
static member Sequential (t: voption<Choice<'T, 'Choice2Of2>>) : Choice<'T voption, 'Choice2Of2> =
391+
match t with
392+
| ValueSome (Choice1Of2 x) -> Choice1Of2 (ValueSome x)
393+
| ValueSome (Choice2Of2 x) -> Choice2Of2 x
394+
| ValueNone -> Choice1Of2 ValueNone
395+
356396

357397
type Result<'T, 'Error> with
358398

@@ -438,4 +478,4 @@ module Extensions =
438478
match t with
439479
| ValueSome (Ok x) -> Ok (ValueSome x)
440480
| ValueNone -> Ok ValueNone
441-
| ValueSome (Error x) -> Error x
481+
| ValueSome (Error x) -> Error x

0 commit comments

Comments
 (0)