Skip to content

Conversation

A4-Tacks
Copy link

Collect all iterator elements into one of two partitions.
Unlike [Iterator::partition], when predicate returns true for the first time,
it collects this element and all rest elements into B.

use itertools::Itertools;

let nums = vec![0, 1, 2, 3, 4, 5];

let (a, b): (Vec<_>, Vec<_>) = nums.into_iter().split(|n| *n == 3);

assert_eq!(a, [0, 1, 2]);
assert_eq!(b, [3, 4, 5]);

Collect all iterator elements into one of two partitions.
Unlike [Itertools::split], when predicate returns true, the element is collected into A

use itertools::Itertools;

let nums = vec![0, 1, 2, 3, 4, 5];

let (a, b): (Vec<_>, Vec<_>) = nums.into_iter().split_inclusive(|n| *n == 3);

assert_eq!(a, [0, 1, 2, 3]);
assert_eq!(b, [4, 5]);

Collect all iterator elements into one of two partitions.
Unlike [`Iterator::partition`], when predicate returns true for the first time,
it collects this element and all rest elements into B.

```rust
use itertools::Itertools;

let nums = vec![0, 1, 2, 3, 4, 5];

let (a, b): (Vec<_>, Vec<_>) = nums.into_iter().split(|n| *n == 3);

assert_eq!(a, [0, 1, 2]);
assert_eq!(b, [3, 4, 5]);
```

---

Collect all iterator elements into one of two partitions.
Unlike [`Itertools::split`], when predicate returns true, the element is collected into A

```rust
use itertools::Itertools;

let nums = vec![0, 1, 2, 3, 4, 5];

let (a, b): (Vec<_>, Vec<_>) = nums.into_iter().split_inclusive(|n| *n == 3);

assert_eq!(a, [0, 1, 2, 3]);
assert_eq!(b, [4, 5]);
```
@phimuemue
Copy link
Member

Hi there. Honestly, I doubt this is really required from an API prospective, because afaik it can be expressed in terms of partition: the predicate can capture an outer bool that is flipped at the "partition point" and be returned. This "trick" might be a bit less efficient but has the advantage that it uses known functions (as opposed to the new split) - and, as a corollary, avoids the question what split should do on the second true (flip to the other container again, or ignore it).

Thus, I suggest to close this.

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.

2 participants