pub struct Traversal<'a, PointerBrand, S, T, A, B, F>where
PointerBrand: ToDynCloneFn,
F: TraversalFunc<'a, S, T, A, B> + 'a,
S: 'a,
T: 'a,
A: 'a,
B: 'a,{
pub traversal: F,
/* private fields */
}Expand description
A polymorphic traversal.
Matches PureScript’s Traversal s t a b.
§Type Parameters
'a: The lifetime of the values.PointerBrand: The reference-counted pointer type.S: The source type of the structure.T: The target type of the structure after an update.A: The source type of the focus.B: The target type of the focus after an update.F: The type of the traversal function.
Fields§
§traversal: FThe traversal function.
In PureScript this is (forall f. Applicative f => (a -> f b) -> s -> f t).
Implementations§
Source§impl<'a, PointerBrand, S, T, A, B, F> Traversal<'a, PointerBrand, S, T, A, B, F>where
PointerBrand: ToDynCloneFn,
F: TraversalFunc<'a, S, T, A, B> + 'a,
§Type Parameters
'a: The lifetime of the values.
PointerBrand: The reference-counted pointer type.
S: The source type of the structure.
T: The target type of the structure after an update.
A: The source type of the focus.
B: The target type of the focus after an update.
F: The type of the traversal function.
impl<'a, PointerBrand, S, T, A, B, F> Traversal<'a, PointerBrand, S, T, A, B, F>where
PointerBrand: ToDynCloneFn,
F: TraversalFunc<'a, S, T, A, B> + 'a,
§Type Parameters
'a: The lifetime of the values.PointerBrand: The reference-counted pointer type.S: The source type of the structure.T: The target type of the structure after an update.A: The source type of the focus.B: The target type of the focus after an update.F: The type of the traversal function.
Sourcepub fn new(traversal: F) -> Self
pub fn new(traversal: F) -> Self
Creates a new Traversal instance.
§Type Signature
forall PointerBrand S T A B F. (ToDynCloneFn PointerBrand, TraversalFunc F) => F -> Traversal PointerBrand S T A B F
§Parameters
traversal: The traversal function.
§Returns
A new instance of the type.
§Examples
use fp_library::{
Apply,
brands::*,
classes::{
Applicative,
lift::Lift,
optics::traversal::TraversalFunc,
pointed::Pointed,
},
kinds::*,
types::optics::Traversal,
};
#[derive(Clone)]
struct ListTraversal;
impl<'a, A: 'a + Clone> TraversalFunc<'a, Vec<A>, Vec<A>, A, A> for ListTraversal {
fn apply<M: Applicative>(
&self,
f: Box<dyn Fn(A) -> Apply!(<M as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, A>) + 'a>,
s: Vec<A>,
) -> Apply!(<M as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, Vec<A>>) {
s.into_iter().fold(M::pure(vec![]), |acc, a| {
M::lift2(
|mut v: Vec<A>, x: A| {
v.push(x);
v
},
acc,
f(a),
)
})
}
}
let traversal = Traversal::<'_, RcBrand, Vec<i32>, Vec<i32>, i32, i32, _>::new(ListTraversal);
assert_eq!(
traversal.traversal.apply::<OptionBrand>(Box::new(|x| Some(x + 1)), vec![1, 2]),
Some(vec![2, 3])
);Trait Implementations§
Source§impl<'a, PointerBrand, S, A, F> FoldOptic<'a, S, A> for Traversal<'a, PointerBrand, S, S, A, A, F>where
PointerBrand: ToDynCloneFn,
F: TraversalFunc<'a, S, S, A, A> + Clone + 'a,
S: 'a,
A: 'a + Clone,
§Type Parameters
'a: The lifetime of the values.
PointerBrand: The reference-counted pointer type.
S: The source type of the structure.
A: The source type of the focus.
F: The type of the traversal function.
impl<'a, PointerBrand, S, A, F> FoldOptic<'a, S, A> for Traversal<'a, PointerBrand, S, S, A, A, F>where
PointerBrand: ToDynCloneFn,
F: TraversalFunc<'a, S, S, A, A> + Clone + 'a,
S: 'a,
A: 'a + Clone,
§Type Parameters
'a: The lifetime of the values.PointerBrand: The reference-counted pointer type.S: The source type of the structure.A: The source type of the focus.F: The type of the traversal function.
Source§fn evaluate<R: 'a + Monoid + Clone + 'static, Q: ToDynCloneFn + 'static>(
&self,
pab: <ForgetBrand<Q, R> as Kind_266801a817966495>::Of<'a, A, A>,
) -> <ForgetBrand<Q, R> as Kind_266801a817966495>::Of<'a, S, S>
fn evaluate<R: 'a + Monoid + Clone + 'static, Q: ToDynCloneFn + 'static>( &self, pab: <ForgetBrand<Q, R> as Kind_266801a817966495>::Of<'a, A, A>, ) -> <ForgetBrand<Q, R> as Kind_266801a817966495>::Of<'a, S, S>
§Type Signature
forall PointerBrand S A F R Q. (Monoid R, ToDynCloneFn Q, ToDynCloneFn PointerBrand, TraversalFunc F) => (&Traversal PointerBrand S A F, Forget Q R A A) -> Forget Q R S S
§Type Parameters
R: The monoid type.Q: The reference-counted pointer type for the Forget brand.
§Parameters
&self: The traversal instance.pab: The profunctor value to transform.
§Returns
The transformed profunctor value.
§Examples
use fp_library::{
Apply,
brands::{
optics::*,
*,
},
classes::{
Applicative,
lift::Lift,
optics::{
traversal::TraversalFunc,
*,
},
pointed::Pointed,
profunctor::*,
},
kinds::*,
types::optics::{
Forget,
Traversal,
},
};
#[derive(Clone)]
struct ListTraversal;
impl<'a, A: 'a + Clone> TraversalFunc<'a, Vec<A>, Vec<A>, A, A> for ListTraversal {
fn apply<M: Applicative>(
&self,
f: Box<dyn Fn(A) -> Apply!(<M as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, A>) + 'a>,
s: Vec<A>,
) -> Apply!(<M as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, Vec<A>>) {
s.into_iter().fold(M::pure(vec![]), |acc, a| {
M::lift2(
|mut v: Vec<A>, x: A| {
v.push(x);
v
},
acc,
f(a),
)
})
}
}
let traversal = Traversal::<'_, RcBrand, Vec<i32>, Vec<i32>, i32, i32, _>::new(ListTraversal);
let f = Forget::<RcBrand, String, i32, i32>::new(|x: i32| x.to_string());
let result = FoldOptic::evaluate(&traversal, f);
assert_eq!(result.run(vec![1, 2]), "12".to_string());Source§impl<'a, Q, PointerBrand, S, T, A, B, F> Optic<'a, Q, S, T, A, B> for Traversal<'a, PointerBrand, S, T, A, B, F>where
Q: Wander,
PointerBrand: ToDynCloneFn,
F: TraversalFunc<'a, S, T, A, B> + Clone + 'a,
S: 'a,
T: 'a,
A: 'a,
B: 'a + Clone,
§Type Parameters
'a: The lifetime of the values.
Q: The profunctor type.
PointerBrand: The reference-counted pointer type.
S: The source type of the structure.
T: The target type of the structure after an update.
A: The source type of the focus.
B: The target type of the focus after an update.
F: The type of the traversal function.
impl<'a, Q, PointerBrand, S, T, A, B, F> Optic<'a, Q, S, T, A, B> for Traversal<'a, PointerBrand, S, T, A, B, F>where
Q: Wander,
PointerBrand: ToDynCloneFn,
F: TraversalFunc<'a, S, T, A, B> + Clone + 'a,
S: 'a,
T: 'a,
A: 'a,
B: 'a + Clone,
§Type Parameters
'a: The lifetime of the values.Q: The profunctor type.PointerBrand: The reference-counted pointer type.S: The source type of the structure.T: The target type of the structure after an update.A: The source type of the focus.B: The target type of the focus after an update.F: The type of the traversal function.
Source§fn evaluate(
&self,
pab: <Q as Kind_266801a817966495>::Of<'a, A, B>,
) -> <Q as Kind_266801a817966495>::Of<'a, S, T>
fn evaluate( &self, pab: <Q as Kind_266801a817966495>::Of<'a, A, B>, ) -> <Q as Kind_266801a817966495>::Of<'a, S, T>
Evaluates the traversal with a profunctor.
§Type Signature
forall Q PointerBrand S T A B F. (Wander Q, ToDynCloneFn PointerBrand, TraversalFunc F) => (&Traversal Q PointerBrand S T A B F, Q A B) -> Q S T
§Parameters
&self: The traversal instance.pab: The profunctor value to transform.
§Returns
The transformed profunctor value.
§Examples
use fp_library::{
Apply,
brands::*,
classes::{
Applicative,
lift::Lift,
optics::{
traversal::TraversalFunc,
*,
},
pointed::Pointed,
profunctor::*,
},
kinds::*,
types::optics::Traversal,
};
#[derive(Clone)]
struct ListTraversal;
impl<'a, A: 'a + Clone> TraversalFunc<'a, Vec<A>, Vec<A>, A, A> for ListTraversal {
fn apply<M: Applicative>(
&self,
f: Box<dyn Fn(A) -> Apply!(<M as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, A>) + 'a>,
s: Vec<A>,
) -> Apply!(<M as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, Vec<A>>) {
s.into_iter().fold(M::pure(vec![]), |acc, a| {
M::lift2(
|mut v: Vec<A>, x: A| {
v.push(x);
v
},
acc,
f(a),
)
})
}
}
let traversal = Traversal::<'_, RcBrand, Vec<i32>, Vec<i32>, i32, i32, _>::new(ListTraversal);
let f = std::rc::Rc::new(|x: i32| x + 1) as std::rc::Rc<dyn Fn(i32) -> i32>;
let result: std::rc::Rc<dyn Fn(Vec<i32>) -> Vec<i32>> =
Optic::<'_, RcFnBrand, _, _, _, _>::evaluate(&traversal, f);
assert_eq!(result(vec![1, 2]), vec![2, 3]);Source§impl<'a, Q, PointerBrand, S, T, A, B, F> SetterOptic<'a, Q, S, T, A, B> for Traversal<'a, PointerBrand, S, T, A, B, F>where
PointerBrand: ToDynCloneFn,
Q: ToDynCloneFn,
F: TraversalFunc<'a, S, T, A, B> + Clone + 'a,
S: 'a,
T: 'a,
A: 'a,
B: 'a + Clone,
§Type Parameters
'a: The lifetime of the values.
Q: The reference-counted pointer type for the setter brand.
PointerBrand: The reference-counted pointer type for the traversal.
S: The source type of the structure.
T: The target type of the structure after an update.
A: The source type of the focus.
B: The target type of the focus after an update.
F: The type of the traversal function.
impl<'a, Q, PointerBrand, S, T, A, B, F> SetterOptic<'a, Q, S, T, A, B> for Traversal<'a, PointerBrand, S, T, A, B, F>where
PointerBrand: ToDynCloneFn,
Q: ToDynCloneFn,
F: TraversalFunc<'a, S, T, A, B> + Clone + 'a,
S: 'a,
T: 'a,
A: 'a,
B: 'a + Clone,
§Type Parameters
'a: The lifetime of the values.Q: The reference-counted pointer type for the setter brand.PointerBrand: The reference-counted pointer type for the traversal.S: The source type of the structure.T: The target type of the structure after an update.A: The source type of the focus.B: The target type of the focus after an update.F: The type of the traversal function.
Source§fn evaluate(
&self,
pab: <FnBrand<Q> as Kind_266801a817966495>::Of<'a, A, B>,
) -> <FnBrand<Q> as Kind_266801a817966495>::Of<'a, S, T>
fn evaluate( &self, pab: <FnBrand<Q> as Kind_266801a817966495>::Of<'a, A, B>, ) -> <FnBrand<Q> as Kind_266801a817966495>::Of<'a, S, T>
§Type Signature
forall Q PointerBrand S T A B F. (ToDynCloneFn PointerBrand, ToDynCloneFn Q, TraversalFunc F) => (&Traversal Q PointerBrand S T A B F, Fn Q A B) -> Fn Q S T
§Parameters
&self: The traversal instance.pab: The profunctor value to transform.
§Returns
The transformed profunctor value.
§Examples
use fp_library::{
Apply,
brands::*,
classes::{
Applicative,
lift::Lift,
optics::{
traversal::TraversalFunc,
*,
},
pointed::Pointed,
profunctor::*,
},
kinds::*,
types::optics::Traversal,
};
#[derive(Clone)]
struct ListTraversal;
impl<'a, A: 'a + Clone> TraversalFunc<'a, Vec<A>, Vec<A>, A, A> for ListTraversal {
fn apply<M: Applicative>(
&self,
f: Box<dyn Fn(A) -> Apply!(<M as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, A>) + 'a>,
s: Vec<A>,
) -> Apply!(<M as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, Vec<A>>) {
s.into_iter().fold(M::pure(vec![]), |acc, a| {
M::lift2(
|mut v: Vec<A>, x: A| {
v.push(x);
v
},
acc,
f(a),
)
})
}
}
let traversal = Traversal::<'_, RcBrand, Vec<i32>, Vec<i32>, i32, i32, _>::new(ListTraversal);
let f = std::rc::Rc::new(|x: i32| x + 1) as std::rc::Rc<dyn Fn(i32) -> i32>;
let result: std::rc::Rc<dyn Fn(Vec<i32>) -> Vec<i32>> =
SetterOptic::<RcBrand, _, _, _, _>::evaluate(&traversal, f);
assert_eq!(result(vec![1, 2]), vec![2, 3]);Source§impl<'a, PointerBrand, S, T, A, B, F> TraversalOptic<'a, S, T, A, B> for Traversal<'a, PointerBrand, S, T, A, B, F>where
PointerBrand: ToDynCloneFn,
F: TraversalFunc<'a, S, T, A, B> + Clone + 'a,
S: 'a,
T: 'a,
A: 'a,
B: 'a + Clone,
§Type Parameters
'a: The lifetime of the values.
PointerBrand: The reference-counted pointer type.
S: The source type of the structure.
T: The target type of the structure after an update.
A: The source type of the focus.
B: The target type of the focus after an update.
F: The type of the traversal function.
impl<'a, PointerBrand, S, T, A, B, F> TraversalOptic<'a, S, T, A, B> for Traversal<'a, PointerBrand, S, T, A, B, F>where
PointerBrand: ToDynCloneFn,
F: TraversalFunc<'a, S, T, A, B> + Clone + 'a,
S: 'a,
T: 'a,
A: 'a,
B: 'a + Clone,
§Type Parameters
'a: The lifetime of the values.PointerBrand: The reference-counted pointer type.S: The source type of the structure.T: The target type of the structure after an update.A: The source type of the focus.B: The target type of the focus after an update.F: The type of the traversal function.
Source§fn evaluate<Q: Wander>(
&self,
pab: <Q as Kind_266801a817966495>::Of<'a, A, B>,
) -> <Q as Kind_266801a817966495>::Of<'a, S, T>
fn evaluate<Q: Wander>( &self, pab: <Q as Kind_266801a817966495>::Of<'a, A, B>, ) -> <Q as Kind_266801a817966495>::Of<'a, S, T>
§Type Signature
forall PointerBrand S T A B F Q. (Wander Q, ToDynCloneFn PointerBrand, TraversalFunc F) => (&Traversal PointerBrand S T A B F, Q A B) -> Q S T
§Type Parameters
Q: The profunctor type.
§Parameters
&self: The traversal instance.pab: The profunctor value to transform.
§Returns
The transformed profunctor value.
§Examples
use fp_library::{
Apply,
brands::*,
classes::{
Applicative,
lift::Lift,
optics::{
traversal::TraversalFunc,
*,
},
pointed::Pointed,
profunctor::*,
},
kinds::*,
types::optics::Traversal,
};
#[derive(Clone)]
struct ListTraversal;
impl<'a, A: 'a + Clone> TraversalFunc<'a, Vec<A>, Vec<A>, A, A> for ListTraversal {
fn apply<M: Applicative>(
&self,
f: Box<dyn Fn(A) -> Apply!(<M as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, A>) + 'a>,
s: Vec<A>,
) -> Apply!(<M as Kind!( type Of<'b, U: 'b>: 'b; )>::Of<'a, Vec<A>>) {
s.into_iter().fold(M::pure(vec![]), |acc, a| {
M::lift2(
|mut v: Vec<A>, x: A| {
v.push(x);
v
},
acc,
f(a),
)
})
}
}
let traversal = Traversal::<'_, RcBrand, Vec<i32>, Vec<i32>, i32, i32, _>::new(ListTraversal);
let f = std::rc::Rc::new(|x: i32| x + 1) as std::rc::Rc<dyn Fn(i32) -> i32>;
let result: std::rc::Rc<dyn Fn(Vec<i32>) -> Vec<i32>> =
TraversalOptic::evaluate::<RcFnBrand>(&traversal, f);
assert_eq!(result(vec![1, 2]), vec![2, 3]);Auto Trait Implementations§
impl<'a, PointerBrand, S, T, A, B, F> Freeze for Traversal<'a, PointerBrand, S, T, A, B, F>where
F: Freeze,
impl<'a, PointerBrand, S, T, A, B, F> RefUnwindSafe for Traversal<'a, PointerBrand, S, T, A, B, F>where
F: RefUnwindSafe,
PointerBrand: RefUnwindSafe,
S: RefUnwindSafe,
T: RefUnwindSafe,
A: RefUnwindSafe,
B: RefUnwindSafe,
impl<'a, PointerBrand, S, T, A, B, F> Send for Traversal<'a, PointerBrand, S, T, A, B, F>
impl<'a, PointerBrand, S, T, A, B, F> Sync for Traversal<'a, PointerBrand, S, T, A, B, F>
impl<'a, PointerBrand, S, T, A, B, F> Unpin for Traversal<'a, PointerBrand, S, T, A, B, F>
impl<'a, PointerBrand, S, T, A, B, F> UnsafeUnpin for Traversal<'a, PointerBrand, S, T, A, B, F>where
F: UnsafeUnpin,
impl<'a, PointerBrand, S, T, A, B, F> UnwindSafe for Traversal<'a, PointerBrand, S, T, A, B, F>where
F: UnwindSafe,
PointerBrand: UnwindSafe,
S: RefUnwindSafe,
T: RefUnwindSafe,
A: RefUnwindSafe,
B: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more