pub trait Associator<P> {
// Required method
fn assoc(self) -> P;
}
Expand description
Rearrange the grouping of tuples.
Although this trait can in principle describe any effective isomorphism
between two types, the implementations in this crate focus on those
describing how tuples can be rearranged trivially. That is, this crate
implements Associator
to perform “flattening” (or “unflattening”)
operations on tuples of few to several elements.
This is helpful in interfacing chains of calls to Iterator::zip
with
several constructors in this module that require iterators over “flat”
tuples.
use matplotlib::commands::assoc;
let x = vec![1, 2, 3_usize];
let y = vec!['a', 'b', 'c' ];
let z = vec![true, false, true ];
let flat: Vec<(usize, char, bool)>
= x.iter().copied()
.zip(y.iter().copied())
.zip(z.iter().copied()) // element type is ((usize, char), bool)
.map(assoc) // ((A, B), C) -> (A, B, C)
.collect();
assert_eq!(flat, vec![(1, 'a', true), (2, 'b', false), (3, 'c', true)]);
// can also be used for unzipping
let ((x2, y2), z2): ((Vec<usize>, Vec<char>), Vec<bool>)
= flat.into_iter().map(assoc).unzip();
assert_eq!(x2, x);
assert_eq!(y2, y);
assert_eq!(z2, z);