//! Implement your own minimal `itertools` crate. use std::collections::HashSet; use std::hash::Hash; /// Iterator that iterates over the given iterator and returns only unique elements. #[derive(Debug)] pub struct Unique { // TODO: remove `_marker` and add necessary fields as you want _marker: std::marker::PhantomData, } impl Iterator for Unique where I::Item: Eq + Hash + Clone, { type Item = I::Item; fn next(&mut self) -> Option { todo!() } } /// Iterator that chains two iterators together. #[derive(Debug)] pub struct Chain { // TODO: remove `_marker` and add necessary fields as you want _marker: std::marker::PhantomData<(I1, I2)>, } impl, I2: Iterator> Iterator for Chain { type Item = T; fn next(&mut self) -> Option { todo!() } } /// Iterator that iterates over given iterator and enumerates each element. #[derive(Debug)] pub struct Enumerate { // TODO: remove `_marker` and add necessary fields as you want _marker: std::marker::PhantomData, } impl Iterator for Enumerate { type Item = (usize, I::Item); fn next(&mut self) -> Option { todo!() } } /// Iterator that zips two iterators together. /// /// If one iterator is longer than the other one, the remaining elements for the longer element /// should be ignored. #[derive(Debug)] pub struct Zip { // TODO: remove `_marker` and add necessary fields as you want _marker: std::marker::PhantomData<(I1, I2)>, } impl Iterator for Zip { type Item = (I1::Item, I2::Item); fn next(&mut self) -> Option { todo!() } } /// My Itertools trait. pub trait MyIterTools: Iterator { /// Returns an iterator that iterates over the `self` and returns only unique elements. fn my_unique(self) -> Unique where Self: Sized, { todo!() } /// Returns an iterator that chains `self` and `other` together. fn my_chain(self, other: I) -> Chain where Self: Sized, { todo!() } /// Returns an iterator that iterates over `self` and enumerates each element. fn my_enumerate(self) -> Enumerate where Self: Sized, { todo!() } /// Returns an iterator that zips `self` and `other` together. fn my_zip(self, other: I) -> Zip where Self: Sized, { todo!() } /// Foldleft for `MyIterTools` fn my_fold(mut self, init: T, mut f: F) -> T where Self: Sized, F: FnMut(Self::Item, T) -> T, { todo!() } } impl MyIterTools for T where T: Iterator {}