itertools icon indicating copy to clipboard operation
itertools copied to clipboard

Feature request: try_unzip

Open jplatte opened this issue 5 years ago • 8 comments

fn try_unzip<A, B, E, FromA, FromB>(self) -> Result<(FromA, FromB), E>
where
    FromA: Default + Extend<A>,
    FromB: Default + Extend<B>,
    Self: Iterator<Item = Result<(A, B), E>>,
{ ... }

As with my previous feature request, I'd be willing to implement this myself if it is considered useful enough for inclusion :)

jplatte avatar Feb 06 '20 11:02 jplatte

I've hit this same sort of issue a couple of this, and I believe there could be a really nice implementation of unzipping. I don't know about Result-type though. I am thinking something like:

let (vec_x,_vec_y, _) = vec![(122, 4214, "A", "1"), (200, 14, "B", "1")].iter().cloned().unzip();

Also something that utilizes .. in that sort of notation.

I'd be willing to help out, if this could be made more concrete?

CGMossa avatar Mar 29 '20 14:03 CGMossa

@CGMossa An unzip that creates more than two collections is an entirely orthogonal feature to the fallible unzip that I've proposed here. It could be implemented as a macro iunzip!, parallel to izip!.

jplatte avatar Mar 29 '20 15:03 jplatte

Thanks. I'll make a new issue.

CGMossa avatar Mar 29 '20 15:03 CGMossa

@CGMossa Do you have a link to the issue? I'm also interested in a multi-unzip.

Boscop avatar Jul 05 '20 18:07 Boscop

@Boscop I don't think that I made an issue.

CGMossa avatar Jul 06 '20 13:07 CGMossa

For future reference, there is an existing issue for that at #362.

jplatte avatar Jul 06 '20 13:07 jplatte

I was looking for iter.try_unzip()? and eventually realized there's a slightly more awkward spelling that works fine: iter.process_results(|iter| iter.unzip())?.

scottlamb avatar Jan 07 '24 22:01 scottlamb

We have try_collect (in conflict with nightly) and some *_ok methods that we might expand a bit but process_results is quite useful for the general case.

Philippe-Cholet avatar Jan 08 '24 08:01 Philippe-Cholet