Crates.io | multizip |
lib.rs | multizip |
version | 0.1.0 |
source | src |
created_at | 2015-05-25 16:20:12.691362 |
updated_at | 2015-12-11 23:54:29.779751 |
description | Zip 3, 4, 5 or more Rust iterators |
homepage | https://github.com/mdup/multizip |
repository | https://github.com/mdup/multizip |
max_upload_size | |
id | 2204 |
size | 17,273 |
Zip 3, 4, 5 or more Rust iterators
With Rust's stdlib you can only zip 2 iterators at a time:
let a: Vec<i8> = vec![0, 1, 2];
let b: Vec<i8> = vec![3, 4, 5];
let c: Vec<i8> = vec![6, 7, 8];
let d: Vec<i8> = vec![9, 10, 11];
let abc = a.iter().zip(b.iter()).zip(c.iter())
.map(|((&aa, &bb), &cc)| aa+bb+cc);
// (( , ), )
// ^~~~~~~~~~^~~~~~^ awkward!
let abcd = a.iter().zip(b.iter()).zip(c.iter()).zip(d.iter())
.map(|(((&aa, &bb), &cc), &dd)| aa+bb+cc+dd);
// ((( , ), ), )
// ^~~~~~~~~~~^~~~~~^~~~~~^ ughhh!
With multizip
, you get a flattened version of zip
:
let abc = multizip::zip3(a.iter(),
b.iter(),
c.iter())
.map(|(&aa, &bb, &cc)| aa+bb+cc))
// ( , , )
// ^~~~~~~~~~~~~~^ oooh!
let abcd = multizip::zip4(a.iter(),
b.iter(),
c.iter(),
d.iter())
.map(|(&aa, &bb, &cc, &dd)| aa+bb+cc+dd)
// ( , , , )
// ^~~~~~~~~~~~~~~~~~~^ sweet!
TODO: upload to crates.io and update here with Cargo instructions.
Rust supports up to 12 variables in a single tuple, so the following are
implemented: zip2()
, zip3()
, zip4()
..., zip12()
.
If you need more than 12, something is probably wrong with your design. Consider something more appropriate than tuples.
multizip::zip2()
over std::iter::zip()
?The only advantage is the symmetry of arguments, e.g. zip2(a.iter(), b.iter())
over a.iter().zip(b.iter())
.
Marc Dupont -- mdup.fr