zipcartesian: iterator that does a cartesian product on two iterators

Essentially just encodes the flatmap of two iterators being tupled
together.  Different to just a standard zip.

Unfortunately we do need the second iterator to implement Clone as we
clone it for each item of the first iterator to generate the flat_map.

Type tetris masturbation has reached a new high here.
This commit is contained in:
2026-04-14 18:03:48 +01:00
committed by oreodave
parent d8e8682ce7
commit f98b151c61
2 changed files with 27 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ mod card;
mod exactsizearr; mod exactsizearr;
mod helper; mod helper;
mod modes; mod modes;
mod zipcartesian;
fn main() { fn main() {
println!("Hello, world!"); println!("Hello, world!");

26
src/zipcartesian.rs Normal file
View File

@@ -0,0 +1,26 @@
use std::iter::Iterator;
pub trait ZipCatersianExt: Iterator {
fn zip_cartesian<B>(
self,
b: B,
) -> impl Iterator<Item = (Self::Item, B::Item)> + Clone
where
Self::Item: Copy,
Self: Sized + Clone,
B: Iterator<Item: Copy> + Clone;
}
impl<I: Iterator<Item: Copy> + Clone> ZipCatersianExt for I {
/// Exhaustive coupling of two iterators.
/// For each x in `self`: for each y in `b`: yield (x, y).
/// b: B must implement `Clone`.
fn zip_cartesian<B: Iterator<Item: Copy> + Clone>(
self,
b: B,
) -> impl Iterator<Item = (Self::Item, B::Item)> + Clone {
self.flat_map(move |a_item| {
b.clone().map(move |b_item| (a_item, b_item))
})
}
}