-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Seq.sequenceResultM)!: Change Ok to Array
- Loading branch information
Showing
3 changed files
with
45 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
namespace FsToolkit.ErrorHandling | ||
|
||
[<RequireQualifiedAccess>] | ||
module Seq = | ||
module FsToolkit.ErrorHandling.Seq | ||
|
||
let sequenceResultM (xs: seq<Result<'t, 'e>>) : Result<'t[], 'e> = | ||
if isNull xs then | ||
nullArg (nameof xs) | ||
|
||
let acc = ResizeArray() | ||
let mutable err = Unchecked.defaultof<_> | ||
let mutable ok = true | ||
use e = xs.GetEnumerator() | ||
|
||
let sequenceResultM (xs: seq<Result<'t, 'e>>) : Result<'t seq, 'e> = | ||
let rec loop xs ts = | ||
match Seq.tryHead xs with | ||
| Some x -> | ||
x | ||
|> Result.bind (fun t -> loop (Seq.tail xs) (t :: ts)) | ||
| None -> | ||
Ok( | ||
List.rev ts | ||
|> List.toSeq | ||
) | ||
while ok | ||
&& e.MoveNext() do | ||
match e.Current with | ||
| Ok r -> acc.Add r | ||
| Error e -> | ||
ok <- false | ||
err <- e | ||
|
||
// Seq.cache prevents double evaluation in Seq.tail | ||
loop (Seq.cache xs) [] | ||
if ok then Ok(acc.ToArray()) else Error err |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters