itertools icon indicating copy to clipboard operation
itertools copied to clipboard

Add implementation of MultiUnzip to Option

Open zekefast opened this issue 1 year ago • 4 comments

Would it be possible to implement MultiUnzip on Option to make something like that possible to implement?

struct A {
    a: u32,
    b: u32,
    c: u32,
}

let opt = Some(A { a: 1, b: 2, c: 3 });
let non: Option<A> = None;

let (a, b, c): (Option<_>, Option<_>, Option<_>) = opt
    .map(|value| (value.a, value.b, value.c))
    .multiunzip(); // a = Some(1); b = Some(2); c = Some(3)

let (a, b, c): (Option<_>, Option<_>, Option<_>) = non
    .map(|value| (value.a, value.b, value.c))
    .multiunzip(); // a = None; b = None; c = None

zekefast avatar Aug 06 '24 10:08 zekefast

That's an elegant example! I'd have to think through this more (or just try it), but I think we could perhaps replace the IT: Iterator bound on the MultiZip impls with IT: IntoIterator.

jswrenn avatar Aug 06 '24 12:08 jswrenn

@jswrenn That would be great! I think there could be issues with Extend as well as it is not implemented for Option if I remember it right.

zekefast avatar Aug 06 '24 17:08 zekefast

Ah, oof that will be an issue, I think.

jswrenn avatar Aug 06 '24 17:08 jswrenn