implement izip! using .zips + .map
@@ -174,14 +174,27 @@ macro_rules! iproduct {
/// # }
/// ```
macro_rules! izip {
- ($I:expr) => (
- (::std::iter::IntoIterator::into_iter($I))
- );
- ($($I:expr),*) => (
- {
- $crate::multizip(($(izip!($I)),*))
- }
- );
+ // @closure creates a tuple-flattening closure for .map() call. usage:
+ // @closure partial_pattern => partial_tuple , rest , of , iterators
+ // eg. izip!( @closure ((a, b), c) => (a, b, c) , dd , ee )
+ ( @closure $p:pat => $tup:expr ) => {
+ |$p| $tup
+ };
+
+ // The "b" identifier is a different identifier on each recursion level thanks to hygiene.
+ ( @closure $p:pat => ( $($tup:tt)* ) , $_iter:expr $( , $tail:expr )* ) => {
+ izip!(@closure ($p, b) => ( $($tup)*, b ) $( , $tail )*)
+ };
+
+ ( $first:expr $( , $rest:expr )* $(,)* ) => {
+ ::std::iter::IntoIterator::into_iter($first)
+ $(
+ .zip($rest)
+ )*
+ .map(
+ izip!(@closure a => (a) $( , $rest )*)
+ )
+ };
}
/// The trait `Itertools`: extra iterator adaptors and methods for iterators.
