-
Notifications
You must be signed in to change notification settings - Fork 62
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
feat(Seq)!: Add sequenceResultA, align sequenceResultM #255
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5840ac4
Roll on '24
bartelink 1694e33
refactor(Seq.sequenceResultM)!: Change Ok to Array
bartelink 5ffe402
docs: sequenceResultM
bartelink d145e21
feat(Seq): sequenceResultA
bartelink 76286cc
f sequenceResultM docs
bartelink d088226
Supress compile error
bartelink 9d38bc2
Fix proposed version
bartelink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Seq.sequenceResultA | ||
|
||
Namespace: `FsToolkit.ErrorHandling` | ||
|
||
## Function Signature | ||
|
||
```fsharp | ||
seq<Result<'a, 'b>> -> Result<'a[], 'b[]> | ||
``` | ||
|
||
This is applicative, collecting all errors. Compare the example below with [sequenceResultM](sequenceResultM.md). | ||
|
||
See also Scott Wlaschin's [Understanding traverse and sequence](https://fsharpforfunandprofit.com/posts/elevated-world-4/). | ||
|
||
## Examples | ||
|
||
### Example 1 | ||
|
||
```fsharp | ||
// string -> Result<int, string> | ||
let tryParseInt str = | ||
match Int32.TryParse str with | ||
| true, x -> Ok x | ||
| false, _ -> Error $"unable to parse '{str}' to integer" | ||
|
||
["1"; "2"; "3"] | ||
|> Seq.map tryParseInt | ||
|> Seq.sequenceResultA | ||
// Ok [| 1; 2; 3 |] | ||
|
||
["1"; "foo"; "3"; "bar"] | ||
|> Seq.map tryParseInt | ||
|> Seq.sequenceResultA | ||
// Error [| "unable to parse 'foo' to integer" | ||
// "unable to parse 'bar' to integer" |] | ||
``` | ||
|
||
### Example 2 | ||
|
||
```fsharp | ||
// int -> Result<bool, string> | ||
let isPrime (x: int) = | ||
if x < 2 then Error $"{x} must be greater than 1" | ||
elif x = 2 then Ok true | ||
else | ||
let rec isPrime' (x : int) (i : int) = | ||
if i * i > x then Ok true | ||
elif x % i = 0 then Ok false | ||
else isPrime' x (i + 1) | ||
isPrime' x 2 | ||
|
||
// seq<int> -> Result<bool, string[]> | ||
let checkIfAllPrime (numbers: seq<int>) = | ||
seq { for x in numbers -> isPrime x } // Result<bool, string> seq | ||
|> Seq.sequenceResultA // Result<bool[], string[]> | ||
|> Result.map (Seq.forall id) // shortened version of '|> Result.map (fun results -> results |> Array.forall (fun x -> x = true))' | ||
|
||
let a = [| 1; 2; 3; 4; 5 |] |> checkIfAllPrime | ||
// Error [| "1 must be greater than 1" |] | ||
|
||
let b = [ 1; 2; 3; 4; 5; 0 ] |> checkIfAllPrime | ||
// Error [| "1 must be greater than 1"; "0 must be greater than 1" |] | ||
|
||
let a = seq { 2; 3; 4; 5 } |> checkIfAllPrime | ||
// Ok false | ||
|
||
let a = seq { 2; 3; 5 } |> checkIfAllPrime | ||
// Ok true | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Seq.sequenceResultM | ||
|
||
Namespace: `FsToolkit.ErrorHandling` | ||
|
||
## Function Signature | ||
|
||
```fsharp | ||
seq<Result<'a, 'b>> -> Result<'a[], 'b> | ||
``` | ||
|
||
This is monadic, stopping on the first error. Compare the example below with [sequenceResultA](sequenceResultA.md). | ||
|
||
See also Scott Wlaschin's [Understanding traverse and sequence](https://fsharpforfunandprofit.com/posts/elevated-world-4/). | ||
|
||
## Examples | ||
|
||
### Example 1 | ||
|
||
```fsharp | ||
// string -> Result<int, string> | ||
let tryParseInt str = | ||
match Int32.TryParse str with | ||
| true, x -> Ok x | ||
| false, _ -> Error $"unable to parse '{str}' to integer" | ||
|
||
["1"; "2"; "3"] | ||
|> Seq.map tryParseInt | ||
|> Seq.sequenceResultM | ||
// Ok [| 1; 2; 3 |] | ||
|
||
seq { "1"; "foo"; "3"; "bar" } | ||
|> Seq.map tryParseInt | ||
|> Seq.sequenceResultM | ||
// Error "unable to parse 'foo' to integer" | ||
``` | ||
|
||
### Example 2 | ||
|
||
```fsharp | ||
// int -> Result<bool, string> | ||
let isPrime (x: int) = | ||
if x < 2 then Error $"{x} must be greater than 1" | ||
elif x = 2 then Ok true | ||
else | ||
let rec isPrime' (x : int) (i : int) = | ||
if i * i > x then Ok true | ||
elif x % i = 0 then Ok false | ||
else isPrime' x (i + 1) | ||
isPrime' x 2 | ||
|
||
// int seq -> Result<bool, string[]> | ||
let checkIfAllPrime (numbers: seq<int>) = | ||
numbers | ||
|> Seq.map isPrime // seq<Result<bool, string>> | ||
|> Seq.sequenceResultM // Result<bool[], string> | ||
|> Result.map (Array.forall id) // shortened version of '|> Result.map (fun bools -> bools |> Array.forall (fun x -> x = true))' | ||
|
||
let a = [ 1; 2; 3; 4; 5 ] |> checkIfAllPrime | ||
// Error [| "1 must be greater than 1" |] | ||
|
||
let b = [| 1; 2; 3; 4; 5; 0 |] |> checkIfAllPrime | ||
// Error [| "1 must be greater than 1" |] | ||
|
||
let a = seq { 2; 3; 4; 5 } |> checkIfAllPrime | ||
// Ok false | ||
|
||
let a = [2; 3; 5;] |> checkIfAllPrime | ||
// Ok true | ||
``` |
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,37 @@ | ||
namespace FsToolkit.ErrorHandling | ||
|
||
[<RequireQualifiedAccess>] | ||
module Seq = | ||
|
||
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 | ||
) | ||
|
||
// Seq.cache prevents double evaluation in Seq.tail | ||
loop (Seq.cache xs) [] | ||
module FsToolkit.ErrorHandling.Seq | ||
|
||
let sequenceResultM (xs: seq<Result<'t, 'e>>) : Result<'t[], 'e> = | ||
bartelink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if isNull xs then | ||
nullArg (nameof xs) | ||
|
||
let acc = ResizeArray() | ||
let mutable err = Unchecked.defaultof<_> | ||
let mutable ok = true | ||
use e = xs.GetEnumerator() | ||
|
||
while ok | ||
&& e.MoveNext() do | ||
match e.Current with | ||
| Ok r -> acc.Add r | ||
| Error e -> | ||
ok <- false | ||
err <- e | ||
|
||
if ok then Ok(acc.ToArray()) else Error err | ||
|
||
let sequenceResultA (xs: seq<Result<'t, 'e>>) : Result<'t[], 'e[]> = | ||
if isNull xs then | ||
nullArg (nameof xs) | ||
|
||
let oks = ResizeArray() | ||
let errs = ResizeArray() | ||
|
||
for x in xs do | ||
match x with | ||
| Ok r -> oks.Add r | ||
| Error e -> errs.Add e | ||
|
||
match errs.ToArray() with | ||
| [||] -> Ok(oks.ToArray()) | ||
| errs -> Error errs |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's a new vulnerability that breaks the build
I see you have 1903 in the build props - if you fix that, I'l rebase off that as its obv not ideal to have this in the PR