[no description]
| -rw-r--r-- | src/iterator.rs | 34 | ||||
| -rw-r--r-- | src/lib.rs | 2 |
2 files changed, 33 insertions, 3 deletions
diff --git a/src/iterator.rs b/src/iterator.rs index e2ed432..9d47e46 100644 --- a/src/iterator.rs +++ b/src/iterator.rs @@ -111,6 +111,34 @@ pub trait IteratorOfTuples: Iterator<Item: Tupl> + Sized { { self.map(move |x| x.map_all_mut(&mut fns)) } + /// filter by item at + /// + /// usage: + /// ```rust + /// use ttools::*; + /// // note the `hrf` call. + /// // its necessary because rust is a sad language. + /// let x = std::iter::once((1, 2)).filter_on::<0>(hrf(|&x| x == 1)).next(); + /// assert_eq!(x, Some((1, 2))); + /// ```` + fn filter_on<const N: usize>( + self, + mut f: impl FnMut( + <<Self::Item as Tupl>::AsRef<'_> as Pick<N>>::At, + ) -> bool, + ) -> core::iter::Filter< + Self, + impl FnMut(&<Self as Iterator>::Item) -> bool, + > + where + for<'a> <Self::Item as Tupl>::AsRef<'a>: Pick<N>, + { + self.filter(move |x| { + let x = x.as_ref(); + let at = x.pick(); + f(at) + }) + } } impl<I: Iterator<Item: Tupl>> IteratorOfTuples for I {} @@ -210,6 +238,10 @@ ref_traits!( as_mut, mut ); +/// Required to call [`IteratorOfTuples::filter_map_on`] +pub fn hrf<T, U, F: for<'a> Fn(&'a T) -> U>(f: F) -> F { + f +} #[test] fn x() { @@ -219,6 +251,4 @@ fn x() { .map_all((|x| x + 1, |x| x.to_uppercase())) .next_chunk::<2>() .unwrap(); - - [(1, 2)].iter().map_on::<0>(|x| x + 2); } @@ -20,7 +20,7 @@ pub use iterator::{ IteratorOfMutableReferencesToTuple, IteratorOfMutableReferencesToTupleWithF, IteratorOfReferencesToTuple, IteratorOfReferencesToTupleWithF, IteratorOfTuples, - IteratorOfTuplesWithF, + IteratorOfTuplesWithF, hrf, }; pub mod fns; mod r#try; |