Skip to main content

Traversal

Struct Traversal 

Source
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: F

The 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.
Source

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.
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>

§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.
Source§

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.
Source§

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.
Source§

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>

§

impl<'a, PointerBrand, S, T, A, B, F> Send for Traversal<'a, PointerBrand, S, T, A, B, F>
where F: Send, PointerBrand: Send, S: Sync, T: Sync, A: Sync, B: Sync,

§

impl<'a, PointerBrand, S, T, A, B, F> Sync for Traversal<'a, PointerBrand, S, T, A, B, F>
where F: Sync, PointerBrand: Sync, S: Sync, T: Sync, A: Sync, B: Sync,

§

impl<'a, PointerBrand, S, T, A, B, F> Unpin for Traversal<'a, PointerBrand, S, T, A, B, F>
where F: Unpin, PointerBrand: Unpin,

§

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>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pipe for T

Source§

fn pipe<B>(self, f: impl FnOnce(Self) -> B) -> B

Pipes self into a function, enabling left-to-right composition. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.