From f98b151c61647c9d77022dd1290e88b2f2a8eb7c Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Tue, 14 Apr 2026 18:03:48 +0100 Subject: [PATCH] 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. --- src/main.rs | 1 + src/zipcartesian.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/zipcartesian.rs diff --git a/src/main.rs b/src/main.rs index bba6ed0..a10e4a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod card; mod exactsizearr; mod helper; mod modes; +mod zipcartesian; fn main() { println!("Hello, world!"); diff --git a/src/zipcartesian.rs b/src/zipcartesian.rs new file mode 100644 index 0000000..932975f --- /dev/null +++ b/src/zipcartesian.rs @@ -0,0 +1,26 @@ +use std::iter::Iterator; + +pub trait ZipCatersianExt: Iterator { + fn zip_cartesian( + self, + b: B, + ) -> impl Iterator + Clone + where + Self::Item: Copy, + Self: Sized + Clone, + B: Iterator + Clone; +} + +impl + 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 + Clone>( + self, + b: B, + ) -> impl Iterator + Clone { + self.flat_map(move |a_item| { + b.clone().map(move |b_item| (a_item, b_item)) + }) + } +}