Skip to content

Commit 5c86c2c

Browse files
committed
Implement partition_result
1 parent 573743a commit 5c86c2c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,6 +2494,33 @@ pub trait Itertools : Iterator {
24942494
(left, right)
24952495
}
24962496

2497+
/// Partition a sequence of `Result`s into one list of all the `Ok` elements
2498+
/// and another list of all the `Err` elements.
2499+
///
2500+
/// ```
2501+
/// use itertools::Itertools;
2502+
///
2503+
/// let successes_and_failures = vec![Ok(1), Err(false), Err(true), Ok(2)];
2504+
///
2505+
/// let (successes, failures): (Vec<_>, Vec<_>) = successes_and_failures
2506+
/// .into_iter()
2507+
/// .partition_result();
2508+
///
2509+
/// assert_eq!(successes, [1, 2]);
2510+
/// assert_eq!(failures, [false, true]);
2511+
/// ```
2512+
fn partition_result<A, B, T, E>(self) -> (A, B)
2513+
where
2514+
Self: Iterator<Item = Result<T, E>> + Sized,
2515+
A: Default + Extend<T>,
2516+
B: Default + Extend<E>,
2517+
{
2518+
self.partition_map(|r| match r {
2519+
Ok(v) => Either::Left(v),
2520+
Err(v) => Either::Right(v),
2521+
})
2522+
}
2523+
24972524
/// Return a `HashMap` of keys mapped to `Vec`s of values. Keys and values
24982525
/// are taken from `(Key, Value)` tuple pairs yielded by the input iterator.
24992526
///

0 commit comments

Comments
 (0)