Skip to content
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

Added partition_map_multiple #488

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jeroenvervaeke
Copy link

Issue

partition_map enables you to split an iterator into 2 separate collections. You can choose if you want to put the current item into the Left collection by returning Either::Left or put it in the Right collection by returning Either::Right.
The limitation of partition_map is that you can only put the result in the Left or the Right collection, but not in both.

Practical example

I have some kind of batch processing pipeline that has a list of tuples.
The definition of these tuples looks like this: (Uuid, Vec<Result<Ok, Error>>).
I want to split this iterator of tuples into 2 vecs (or hashmaps) of tuples: (Uuid, Vec<Ok>) and (Uuid, Vec<Err>).

This is where partition_map_multiple comes in.
If the tuple contains no errors I return EitherOrBoth::Left, in case the tuple contains no oks I return EitherOrBoth::Right, in case my tuple contains both errors and oks I return EitherOrBoth::Both.

Remarks

  • I used this extension quiet a lot in a batch processing application I wrote. I'm not sure if this extension fits into this crate, all feedback is welcome.
  • I did not alter the partition_map function to avoid breakage

@phimuemue
Copy link
Member

I see that this may have some value. However, if we decide to accept it (probably @jswrenn's call), I'd vote to investigate if we could generalize this to not only support EitherOrBoth, but also tuples, such as (Option<A>, Option<B>, Option<C>) - which would then collect the Somes into three respective containers.

I think it is feasible to implement it (probably up to the maximum number of tuple elements that we use in other places, too): See here for a draft. We could use macros to generate the tuple implementations, and possibly even express the EitherOrBoth-version in terms of the (Option, Option)-version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants