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

Implement ParallelExtend for tuples #600

Closed
semtexzv opened this issue Oct 5, 2018 · 1 comment
Closed

Implement ParallelExtend for tuples #600

semtexzv opened this issue Oct 5, 2018 · 1 comment

Comments

@semtexzv
Copy link

semtexzv commented Oct 5, 2018

I am currently working on a project , that needs to recursively split the input data to several categories.
Using ParallelIterator::partition_map i can split the input data into 2, but this is not recursive,
If for example I return Either<Either<A,B>,C> from my iterator, I am unable to partition the data into
((Vec<A>,Vec<B>),Vec<C>).

So far I have been unable to implement this on my own, and would appreciate help.

My idea was to implement this trait for tuples, something like this


impl<R, S, T> ParallelExtend<T> for (R, S)
    where R: ParallelExtend<T>,
               S: ParallelExtend<T>
    {
        fn par_extend<I>(&mut self, par_iter: I) where I: IntoParallelIterator<Item=Either<R, S>> {
    }
}

I need this functionality to partition the input data into several containers with heterogenous types, and do it as fast as possible.

@cuviper
Copy link
Member

cuviper commented Oct 5, 2018

I think the signature you need is actually like this:

impl<L, R, T, U> ParallelExtend<Either<L, R>> for (T, U)
where
    L: Send,
    R: Send,
    T: ParallelExtend<L>,
    U: ParallelExtend<R>,
{
    fn par_extend<I>(&mut self, par_iter: I)
    where
        I: IntoParallelIterator<Item = Either<L, R>>,
    {
        unimplemented!()
    }
}

My first thought when reading the subject of this issue was that it would be more like unzip:

impl<A, B, T, U> ParallelExtend<(A, B)> for (T, U)
where
    A: Send,
    B: Send,
    T: ParallelExtend<A>,
    U: ParallelExtend<B>,
{
    fn par_extend<I>(&mut self, par_iter: I)
    where
        I: IntoParallelIterator<Item = (A, B)>,
    {
        unimplemented!()
    }
}

It looks like rustc is happy to let these co-exist!

bors bot added a commit that referenced this issue Oct 30, 2018
604: impl ParallelExtend for tuple pairs r=nikomatsakis a=cuviper

`ParallelExtend<(A, B)>` and `ParallelExtend<Either<L, R>>` for tuples
behave like `unzip` and `partition_map` respectively.  These allow the
possibility of nested `unzip` and `partition_map` operations, filling
into more than just two collections.  For instance, `(A, (B, C))` items
can be unzipped into `(Vec<A>, (Vec<B>, Vec<C>))`.

Fixes #600.

Co-authored-by: Josh Stone <cuviper@gmail.com>
@bors bors bot closed this as completed in #604 Oct 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants