use crate::{ lenses::{Lens, LensOver, LensView}, prisms::Prism, traversals::{Traversal, TraversalOver, TraversalTraverse}, }; #[derive(Clone, Copy)] pub struct Combination(A, B); // additions // lens + lens impl std::ops::Add> for Lens { type Output = Lens, Lens>>; fn add(self, rhs: Lens) -> Self::Output { Lens(Combination(self, rhs)) } } // traversal + traversal impl std::ops::Add> for Traversal { type Output = Traversal, Traversal>>; fn add(self, rhs: Traversal) -> Self::Output { Traversal(Combination(self, rhs)) } } // traversal + lens impl std::ops::Add> for Traversal { type Output = Traversal, Traversal>>>; fn add(self, rhs: Lens) -> Self::Output { Traversal(Combination(self, rhs.to_traversal())) } } // lens + traversal impl std::ops::Add> for Lens { type Output = Traversal>, Traversal>>; fn add(self, rhs: Traversal) -> Self::Output { Traversal(Combination(self.to_traversal(), rhs)) } } // traversal + prism impl std::ops::Add> for Traversal { type Output = Traversal, Traversal>>>; fn add(self, rhs: Prism) -> Self::Output { Traversal(Combination(self, rhs.to_traversal())) } } // prism + traversal impl std::ops::Add> for Prism { type Output = Traversal>, Traversal>>; fn add(self, rhs: Traversal) -> Self::Output { Traversal(Combination(self.to_traversal(), rhs)) } } // trait impls for Combination impl LensView for Combination where A: LensView, B: LensView, { type Field = B::Field; fn view(&self, thing: T) -> Self::Field { B::view(&self.1, A::view(&self.0, thing)) } } impl LensOver for Combination where A: LensOver, B: LensOver, { fn over(&self, thing: T, f: F) -> T where F: FnOnce(Self::Field) -> Self::Field, { A::over(&self.0, thing, |b| B::over(&self.1, b, f)) } } impl TraversalTraverse for Combination where A: TraversalTraverse, B: TraversalTraverse, { type Field = B::Field; fn traverse(&self, thing: T) -> Vec { let a = A::traverse(&self.0, thing); a.into_iter() .map(|v| B::traverse(&self.1, v)) .flatten() .collect() } } impl TraversalOver for Combination where A: TraversalOver, B: TraversalOver, { fn over(&self, thing: T, mut f: F) -> T where F: FnMut(Self::Field) -> Self::Field, { A::over(&self.0, thing, |b| B::over(&self.1, b, &mut f)) } } #[cfg(test)] mod tests { use crate::{ lenses::{_0, _1}, prisms::_Some, traversals::each, }; #[test] fn can_view_lens_combination() { let a = ((1, 2), 3); let lens = _0 + _1; let a = lens(a); assert_eq!(a, 2); } #[test] fn can_over_lens_combination() { let a = ((1, 2), 3); let lens = _0 + _1; let a = lens(a, |v| v + 1); assert_eq!(a, ((1, 3), 3)); } #[test] fn can_combine_traversals() { let array = [vec![1, 2], vec![3, 4]]; // combine two traversals let res = (each + each)(array, |v| v + 1); assert_eq!(res, [vec![2, 3], vec![4, 5]]); } #[test] fn can_combine_traversal_with_lens() { let array = [(1, 2), (3, 4), (5, 6)]; // combine a traversal with a lens let t = each + _0; // traverse let res = t(array); assert_eq!(res, vec![1, 3, 5]); // over let res = t(array, |v| v + 1); assert_eq!(res, [(2, 2), (4, 4), (6, 6)]); } #[test] fn can_combine_lens_with_traversal() { let array = [(1, 2), (3, 4), (5, 6)]; // combine a traversal with a lens let t = _0 + each; // traverse let res = t(array); assert_eq!(res, vec![1, 2]); // over let res = t(array, |v| v + 1); assert_eq!(res, [(2, 3), (3, 4), (5, 6)]); } #[test] fn can_combine_prism_with_traversal() { let array = [Some(1), None, Some(3), None, Some(5)]; // combine a traversal with a lens let t = each + _Some; // traverse let res = t(array); assert_eq!(res, vec![1, 3, 5]); // over let res = t(array, |v| v + 1); assert_eq!(res, [Some(2), None, Some(4), None, Some(6)]); } #[test] fn can_combine_traversal_with_prism() { let array = Some([1, 2, 3]); // combine a traversal with a lens let t = _Some + each; // traverse let res = t(array); assert_eq!(res, vec![1, 2, 3]); // over let res = t(array, |v| v + 1); assert_eq!(res, Some([2, 3, 4])); let array: Option<[i32; 3]> = None; // traverse let res = t(array); assert_eq!(res, vec![]); // over let res = t(array, |v| v + 1); assert_eq!(res, None); } }