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.
Creating a new PR for this.
Personally I have run into situations where I have a list of options or a list of results and it kind of hurts my brain figuring out how to "invert/transpose" it. So this PR converts an
array<result<'ok,'err>>
to aresult<array<'ok>,'err>
. Among functional programming nerds I found out this istraverse
andsequence
. The F# article explains these concepts. There are flavors of this kind of function where you get all the errors, which seems less useful. I think the terms "sequence" and "traverse" are a bit technical and prefer the terms I used -fromArrayMap
andfromArray
. I think this is a good function to have eventually. For completeness we'd want to think through versions for lists and options. Transforming anarray<option<'a>>
to anoption<array<'a>>
is handy. And definitely transforming one option to one result and vice versa is useful.https://fsharpforfunandprofit.com/posts/elevated-world-4/#traverse
https://gcanti.github.io/fp-ts/modules/Traversable.ts.html
https://stackoverflow.com/questions/50789065/turn-list-of-result-into-result-of-list-inside-a-computation-expression
fp-ts and Haskell and other tools for functional programming with type classes let you do this kind of thing. F# doesn't have this. The Rust standard library lets you transform a result of an option into an option of a result.
@glennsl proposed this implementation but acknowledged he hadn't tested it.