Skip to content

Question/Feature: partition collections of results into ok cases and error cases. #358

@bpe-incom

Description

@bpe-incom

When dealing with collections of Results it can in some cases be useful partition Ok cases from Error cases. For instance when doing something that can error partially during batch processing or similar I have found myself having to work with a Result<'A, 'B>[] where it would make a lot of sense to return a data structure indicating "Partial Success" and including the Ok cases and Error cases separately.

Therefore, I would find it very useful to have a partition function which also destructures the result types such that I can deal with both ok cases and error cases. I have solved this using this implementation which I thought might be nice to have in this project. The below implementation should of course be extended to other collections as well.

[<RequireQualifiedAccess>]
module Array =
    let partitionResults (input: Result<'a,'b>[]) : 'a[] * 'b[] =                                                                                                                                               
      input |> Array.choose (function Ok v -> Some v | _ -> None),                                                                                                                                            
      input |> Array.choose (function Error e -> Some e | _ -> None) 

Perhaps there is a smarter way of doing this, and if so, I would love to be enlightened.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions