[no description]
its not great. its really not.
| -rw-r--r-- | src/fns.rs | 17 | ||||
| -rw-r--r-- | src/iterator.rs | 16 | ||||
| -rw-r--r-- | src/lib.rs | 2 |
3 files changed, 26 insertions, 9 deletions
diff --git a/src/fns.rs b/src/fns.rs new file mode 100644 index 0000000..f4866e4 --- /dev/null +++ b/src/fns.rs @@ -0,0 +1,17 @@ +use core::ops::Add; + +use crate::{Pick, RePick, Tupl}; + +pub fn on<const N: usize, U, T: Tupl + Pick<N>>( + mut f: impl FnMut(T::At) -> U, +) -> impl FnMut(T) -> U { + move |x| f(x.pick()) +} +pub fn at<const N: usize, U, T: Tupl + RePick<N, U>>( + mut f: impl FnMut(T::At) -> U, +) -> impl FnMut(T) -> T::New { + move |x| { + let (l, a, r) = x.depict(); + T::unpick(l, f(a), r) + } +} diff --git a/src/iterator.rs b/src/iterator.rs index f288943..56a441a 100644 --- a/src/iterator.rs +++ b/src/iterator.rs @@ -1,5 +1,5 @@ #![allow(nonstandard_style)] -use crate::{MapAllMut, Pick, RePick, Tupl}; +use crate::{MapAllMut, Pick, RePick, Tupl, fns}; pub trait IteratorOfTuplesWithF<retval>: Iterator<Item: Tupl> + Sized { @@ -14,11 +14,7 @@ pub trait IteratorOfTuplesWithF<retval>: where Self::Item: RePick<N, retval>, { - self.map(move |x| { - let (l, at, r) = x.depict(); - let at = f(at); - Self::Item::unpick(l, at, r) - }) + self.map(fns::at::<N, _, _>(f)) } /// filter map the item at: (|(x, y)| f(x).zip(Some(y)) fn filter_map_at<const N: usize>( @@ -173,12 +169,12 @@ pub trait $name2<'a, T: Tupl + 'a, retval>: self.$convert().map_at(f) } /// map the item to just one, then map that one: (|x| f(x.0)) - fn map_on<const N: usize, U>( + fn map_on<const N: usize>( self, - f: impl FnMut(<T::$ref<'a> as Pick<N>>::At) -> U, + f: impl FnMut(<T::$ref<'a> as Pick<N>>::At) -> retval, ) -> core::iter::Map< impl Iterator<Item = <T::$ref<'a> as Pick<N>>::At>, - impl FnMut(<T::$ref<'a> as Pick<N>>::At) -> U, + impl FnMut(<T::$ref<'a> as Pick<N>>::At) -> retval, > where T::$ref<'a>: Pick<N>, @@ -223,4 +219,6 @@ fn x() { .map_all((|x| x + 1, |x| x.to_uppercase())) .next_chunk::<2>() .unwrap(); + + [(1, 2)].iter().map_on::<0>(|x| x + 2); } @@ -1,4 +1,5 @@ #![feature( + type_alias_impl_trait, tuple_trait, import_trait_associated_functions, never_type, @@ -18,6 +19,7 @@ pub use iterator::{ IteratorOfReferencesToTupleWithF, IteratorOfTuples, IteratorOfTuplesWithF, }; +pub mod fns; /// Reverse a tuple. pub trait Reverse: Tupl { type Reversed: Tupl; |