Rust: Allow the `Iterator::partition` method to collect in two different collections

Created on 17 Sep 2018  路  2Comments  路  Source: rust-lang/rust

The current Iterator::partition signature look like this:

fn partition<B, F>(self, mut f: F) -> (B, B) where
        Self: Sized,
        B: Default + Extend<Self::Item>,
        F: FnMut(&Self::Item) -> bool
    {
        // ...
    }

Which force the user to collect in two collections of the same type for the two different "sides".

A partition method with a signature like the following, allow more flexibility, allowing to specify the two collections independently.

fn partition<B, C, F>(self, mut f: F) -> (B, C) where
        Self: Sized,
        B: Default + Extend<Self::Item>,
        C: Default + Extend<Self::Item>,
        F: FnMut(&Self::Item) -> bool
    {
        // ...
    }

This is something that could not be changed in a snap of a finger, this is not backward compatible in a sense that the new function signature asks for 3 generic types. It could have been fixed by a default generic (i.e. C=B) but it is not allowed in function generic parameters.

So this issue will be here for a moment, I think. however, I have done the update just to see the compatibility problem.

The other solution could be to add something like Iterator::partition_distinct.

T-libs

Most helpful comment

If we could write something like:

fn partition<B, F, C = B>(self, mut f: F) -> (B, C) ...

then we could do this fully backwards compatibly.

Unfortunately we don't have that ability yet.

All 2 comments

If we could write something like:

fn partition<B, F, C = B>(self, mut f: F) -> (B, C) ...

then we could do this fully backwards compatibly.

Unfortunately we don't have that ability yet.

Itertools has partition_map() as either a substitute or precedent.

Was this page helpful?
0 / 5 - 0 ratings