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:
@@ -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
26
src/zipcartesian.rs
Normal 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))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user