add _init and _last

main
annieversary 2021-11-12 19:24:25 +00:00
parent c3d76f109f
commit 0ec197ff1e
3 changed files with 224 additions and 0 deletions

98
src/traversals/init.rs Normal file
View File

@ -0,0 +1,98 @@
use crate::traversals::{Traversal, TraversalOver, TraversalTraverse};
#[derive(Clone, Copy)]
pub struct InitInner;
#[allow(non_upper_case_globals)]
pub const _init: Traversal<InitInner> = Traversal(InitInner);
macro_rules! make_tuples {
(( $( $v:ident ),* ), ( $( $t:ident ),* ) ) => {
impl<T> TraversalTraverse<( $($t,)* )> for InitInner {
type Field = T;
fn traverse(&self, ( $($v,)* _last ): ($($t,)*)) -> Vec<Self::Field> {
vec![ $($v,)* ]
}
}
impl<T> TraversalOver<( $($t,)* )> for InitInner {
fn over<F>(
&self,
($($v,)* last): ($($t,)*),
mut f: F
) -> ( $($t,)* )
where
F: FnMut(Self::Field) -> Self::Field
{
( $(f($v),)* last )
}
}
impl<'a, T> TraversalTraverse<&'a ( $($t,)* )> for InitInner {
type Field = &'a T;
fn traverse(&self, ($($v,)* _last): &'a ($($t,)*)) -> Vec<Self::Field> {
vec![ $($v,)* ]
}
}
impl<'a, T> TraversalTraverse<&'a mut ( $($t,)* )> for InitInner {
type Field = &'a mut T;
fn traverse(&self, ($($v,)* _last): &'a mut ($($t,)*)) -> Vec<Self::Field> {
vec![ $($v,)* ]
}
}
};
}
make_tuples!((_u), (T, T));
make_tuples!((_u, _v), (T, T, T));
make_tuples!((_u, _v, _w), (T, T, T, T));
make_tuples!((_u, _v, _w, _x), (T, T, T, T, T));
make_tuples!((_u, _v, _w, _x, _y), (T, T, T, T, T, T));
make_tuples!((_u, _v, _w, _x, _y, _z), (T, T, T, T, T, T, T));
macro_rules! make_arrays {
($n:expr, [$( $v:ident ),*]) => {
impl<T> TraversalTraverse<[T; $n]> for InitInner {
type Field = T;
fn traverse(&self, [ $($v,)* _last]: [T; $n]) -> Vec<Self::Field> {
vec![ $($v,)* ]
}
}
impl<T> TraversalOver<[T; $n]> for InitInner {
fn over<F>(
&self,
[ $($v,)* last]: [T; $n],
mut fun: F
) -> [T; $n]
where
F: FnMut(Self::Field) -> Self::Field
{
[$(fun($v),)* last]
}
}
impl<'a, T> TraversalTraverse<&'a [T; $n]> for InitInner {
type Field = &'a T;
fn traverse(&self, [$($v,)* _last]: &'a [T; $n]) -> Vec<Self::Field> {
vec![ $($v,)* ]
}
}
impl<'a, T> TraversalTraverse<&'a mut [T; $n]> for InitInner {
type Field = &'a mut T;
fn traverse(&self, [$($v,)* _last ]: &'a mut [T; $n]) -> Vec<Self::Field> {
vec![ $($v,)* ]
}
}
};
}
make_arrays!(2, [_a]);
make_arrays!(3, [_a, _b]);
make_arrays!(4, [_a, _b, _c]);
make_arrays!(5, [_a, _b, _c, _d]);
make_arrays!(6, [_a, _b, _c, _d, _e]);
make_arrays!(7, [_a, _b, _c, _d, _e, _g]);

99
src/traversals/last.rs Normal file
View File

@ -0,0 +1,99 @@
use crate::traversals::{Traversal, TraversalOver, TraversalTraverse};
#[derive(Clone, Copy)]
pub struct LastInner;
#[allow(non_upper_case_globals)]
pub const _last: Traversal<LastInner> = Traversal(LastInner);
macro_rules! make_tuples {
(( $( $v:ident ),* ), ( $( $t:ident ),* ) ) => {
impl<T> TraversalTraverse<( $($t,)* )> for LastInner {
type Field = T;
fn traverse(&self, ( $($v,)* last ): ($($t,)*)) -> Vec<Self::Field> {
vec![ last ]
}
}
impl<T> TraversalOver<( $($t,)* )> for LastInner {
fn over<F>(
&self,
($($v,)* last): ($($t,)*),
mut f: F
) -> ( $($t,)* )
where
F: FnMut(Self::Field) -> Self::Field
{
( $($v,)* f(last), )
}
}
impl<'a, T> TraversalTraverse<&'a ( $($t,)* )> for LastInner {
type Field = &'a T;
fn traverse(&self, ($($v,)* last): &'a ($($t,)*)) -> Vec<Self::Field> {
vec![ last ]
}
}
impl<'a, T> TraversalTraverse<&'a mut ( $($t,)* )> for LastInner {
type Field = &'a mut T;
fn traverse(&self, ($($v,)* last): &'a mut ($($t,)*)) -> Vec<Self::Field> {
vec![ last ]
}
}
};
}
make_tuples!((_u), (T, T));
make_tuples!((_u, _v), (T, T, T));
make_tuples!((_u, _v, _w), (T, T, T, T));
make_tuples!((_u, _v, _w, _x), (T, T, T, T, T));
make_tuples!((_u, _v, _w, _x, _y), (T, T, T, T, T, T));
make_tuples!((_u, _v, _w, _x, _y, _z), (T, T, T, T, T, T, T));
macro_rules! make_arrays {
($n:expr, [$( $v:ident ),*]) => {
impl<T> TraversalTraverse<[T; $n]> for LastInner {
type Field = T;
fn traverse(&self, [ $($v,)* last]: [T; $n]) -> Vec<Self::Field> {
vec![ last ]
}
}
impl<T> TraversalOver<[T; $n]> for LastInner {
fn over<F>(
&self,
[ $($v,)* last]: [T; $n],
mut fun: F
) -> [T; $n]
where
F: FnMut(Self::Field) -> Self::Field
{
[$(($v),)* fun(last)]
}
}
impl<'a, T> TraversalTraverse<&'a [T; $n]> for LastInner {
type Field = &'a T;
fn traverse(&self, [$($v,)* last]: &'a [T; $n]) -> Vec<Self::Field> {
vec![ last ]
}
}
impl<'a, T> TraversalTraverse<&'a mut [T; $n]> for LastInner {
type Field = &'a mut T;
fn traverse(&self, [ $($v,)* last ]: &'a mut [T; $n]) -> Vec<Self::Field> {
vec![ last ]
}
}
};
}
make_arrays!(1, []);
make_arrays!(2, [_a]);
make_arrays!(3, [_a, _b]);
make_arrays!(4, [_a, _b, _c]);
make_arrays!(5, [_a, _b, _c, _d]);
make_arrays!(6, [_a, _b, _c, _d, _e]);
make_arrays!(7, [_a, _b, _c, _d, _e, _g]);

View File

@ -9,6 +9,11 @@ pub use head::_head;
pub mod tail; pub mod tail;
pub use tail::_tail; pub use tail::_tail;
pub mod init;
pub use init::_init;
pub mod last;
pub use last::_last;
use crate::{ use crate::{
lenses::{Lens, LensOver, LensView}, lenses::{Lens, LensOver, LensView},
prisms::{Prism, PrismPreview}, prisms::{Prism, PrismPreview},
@ -181,4 +186,26 @@ mod tests {
let res = _tail(array, |v| v + 1); let res = _tail(array, |v| v + 1);
assert_eq!(res, [1, 3, 4, 5]); assert_eq!(res, [1, 3, 4, 5]);
} }
#[test]
fn traverse_init_works_on_arrays() {
let array = [1, 2, 3, 4];
let res = _init(array);
assert_eq!(res, vec![1, 2, 3]);
let res = _init(array, |v| v + 1);
assert_eq!(res, [2, 3, 4, 4,]);
}
#[test]
fn traverse_last_works_on_arrays() {
let array = [1, 2, 3, 4];
let res = _last(array);
assert_eq!(res, vec![4]);
let res = _last(array, |v| v + 1);
assert_eq!(res, [1, 2, 3, 5]);
}
} }