itertools
itertools copied to clipboard
Please add fallible version of `partition_map`
Please add a fallible version of partition_map that allows the closure to return Result<Either<L, R>, E>.
It would be easy, by using the for loop again and write match predicate(val)? :)
https://github.com/rust-itertools/itertools/pull/366/files#diff-b1a35a68f14e696205874893c07fd24fdb88882b47c23cc0e0c80a30c7d53759L2009-L2014
fn try_partition_map<A, B, F, L, R, E, I>(it: I, mut f: F) -> Result<(A, B), E>
where
A: Default + Extend<L>,
B: Default + Extend<R>,
F: FnMut(I::Item) -> Result<Either<L, R>, E>,
I: IntoIterator,
{
let mut a = A::default();
let mut b = B::default();
it.into_iter().try_for_each(|x| {
match f(x)? {
Left(l) => a.extend(Some(l)),
Right(r) => b.extend(Some(r)),
}
Ok(())
})?;
Ok((a, b))
}
I can open a PR if this is a desired addition.
Regardless whether we decide to accept this or not, the implementation looks reasonably similar to the existing partition_map (which is good imho). I am not aware that one could use the existing partition_map to easily express what you wrote (or am I wrong?), so there may be some value to it.
Maybe we could name the variables similar to the ones in partition_map, underlining the similarities.