Skip to main content

IndexedFold

Struct IndexedFold 

Source
pub struct IndexedFold<'a, PointerBrand, I, S, T, A, B, F>
where F: IndexedFoldFunc<'a, I, S, A> + 'a,
{ pub fold_fn: F, /* private fields */ }
Expand description

A polymorphic indexed fold.

§Type Parameters

  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • I: The index type.
  • S: The source type of the structure.
  • T: The target type of the structure.
  • A: The source type of the focus.
  • B: The target type of the focus.
  • F: The fold function type.

Fields§

§fold_fn: F

The fold function.

Implementations§

Source§

impl<'a, PointerBrand, I, S, T, A, B, F> IndexedFold<'a, PointerBrand, I, S, T, A, B, F>
where F: IndexedFoldFunc<'a, I, S, A> + 'a,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • I: The index type.
  • S: The source type of the structure.
  • T: The target type of the structure.
  • A: The source type of the focus.
  • B: The target type of the focus.
  • F: The fold function type.
Source

pub fn new(fold_fn: F) -> Self

Create a new indexed fold.

§Type Signature

forall PointerBrand I S T A B F. IndexedFoldFunc F => F -> IndexedFold PointerBrand I S T A B F

§Parameters
  • fold_fn: The fold function.
§Returns

A new IndexedFold instance.

§Examples
use fp_library::{
	brands::{
		RcBrand,
		optics::*,
	},
	classes::{
		IndexedFoldOptic,
		ToDynCloneFn,
		monoid::Monoid,
		semigroup::Semigroup,
	},
	types::optics::*,
};
#[derive(Clone)]
struct MyFold;
impl<'a> IndexedFoldFunc<'a, usize, Vec<i32>, i32> for MyFold {
	fn apply<R: 'a + Monoid + 'static>(
		&self,
		f: Box<dyn Fn(usize, i32) -> R + 'a>,
		s: Vec<i32>,
	) -> R {
		s.into_iter()
			.enumerate()
			.fold(R::empty(), |acc, (i, x)| Semigroup::append(acc, f(i, x)))
	}
}
let l: IndexedFold<RcBrand, usize, Vec<i32>, Vec<i32>, i32, i32, MyFold> =
	IndexedFold::new(MyFold);
let f = Forget::<RcBrand, String, (usize, i32), i32>::new(|(i, x)| format!("[{}]={}", i, x));
let pab = Indexed::new(f);
let result = IndexedFoldOptic::evaluate::<String, RcBrand>(&l, pab);
assert_eq!(result.run(vec![10, 20]), "[0]=10[1]=20");
Source§

impl<'a, PointerBrand, I, Brand, A> IndexedFold<'a, PointerBrand, I, <Brand as Kind_cdc7cd43dac7585f>::Of<'a, A>, <Brand as Kind_cdc7cd43dac7585f>::Of<'a, A>, A, A, Folded<Brand>>
where Brand: FoldableWithIndex<Index = I>, A: 'a + Clone, I: 'a,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The profunctor type.
  • I: The index type.
  • Brand: The brand of the foldable structure.
  • A: The type of the elements in the structure.
Source

pub fn folded() -> Self

Create an indexed fold from a FoldableWithIndex.

§Type Signature

forall PointerBrand I Brand A. FoldableWithIndex Brand => () -> IndexedFold PointerBrand I Brand A

§Returns

A new IndexedFold instance.

§Examples
use fp_library::{
	brands::{
		RcBrand,
		VecBrand,
		optics::*,
	},
	functions::optics_indexed_fold_map,
	types::optics::{
		Folded,
		IndexedFold,
	},
};
let l: IndexedFold<RcBrand, usize, Vec<i32>, Vec<i32>, i32, i32, Folded<VecBrand>> =
	IndexedFold::folded();
let v = vec![10, 20];
let s =
	optics_indexed_fold_map::<RcBrand, _, _, _, String>(&l, |i, x| format!("{}:{}", i, x), v);
assert_eq!(s, "0:101:20".to_string());

Trait Implementations§

Source§

impl<'a, PointerBrand, I, S, T, A, B, F> Clone for IndexedFold<'a, PointerBrand, I, S, T, A, B, F>
where F: IndexedFoldFunc<'a, I, S, A> + Clone + 'a,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • I: The index type.
  • S: The source type of the structure.
  • T: The target type of the structure.
  • A: The source type of the focus.
  • B: The target type of the focus.
  • F: The fold function type.
Source§

fn clone(&self) -> Self

§Type Signature

forall PointerBrand I S T A B F. IndexedFoldFunc F => &IndexedFold PointerBrand I S T A B F -> IndexedFold PointerBrand I S T A B F

§Returns

A new IndexedFold instance that is a copy of the original.

§Examples
use fp_library::{
	brands::{
		RcBrand,
		optics::*,
	},
	classes::{
		IndexedFoldOptic,
		ToDynCloneFn,
		monoid::Monoid,
		semigroup::Semigroup,
	},
	types::optics::*,
};
#[derive(Clone)]
struct MyFold;
impl<'a> IndexedFoldFunc<'a, usize, Vec<i32>, i32> for MyFold {
	fn apply<R: 'a + Monoid + 'static>(
		&self,
		f: Box<dyn Fn(usize, i32) -> R + 'a>,
		s: Vec<i32>,
	) -> R {
		s.into_iter()
			.enumerate()
			.fold(R::empty(), |acc, (i, x)| Semigroup::append(acc, f(i, x)))
	}
}
let l: IndexedFold<RcBrand, usize, Vec<i32>, Vec<i32>, i32, i32, MyFold> =
	IndexedFold::new(MyFold);
let cloned = l.clone();
let f = Forget::<RcBrand, String, (usize, i32), i32>::new(|(i, x)| format!("[{}]={}", i, x));
let pab = Indexed::new(f);
let result = IndexedFoldOptic::evaluate::<String, RcBrand>(&cloned, pab);
assert_eq!(result.run(vec![10, 20]), "[0]=10[1]=20");
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, PointerBrand, I: 'a, S: 'a, T: 'a, A: 'a, B: 'a, F> IndexedFoldOptic<'a, I, S, A> for IndexedFold<'a, PointerBrand, I, S, T, A, B, F>
where F: IndexedFoldFunc<'a, I, S, A> + Clone + 'a, PointerBrand: ToDynCloneFn,

§Type Parameters
  • 'a: The lifetime of the values.
  • PointerBrand: The reference-counted pointer type.
  • I: The index type.
  • S: The source type of the structure.
  • T: The target type of the structure.
  • A: The source type of the focus.
  • B: The target type of the focus.
  • F: The fold function type.
Source§

fn evaluate<R: 'a + Monoid + 'static, Q: ToDynCloneFn + 'static>( &self, pab: Indexed<'a, ForgetBrand<Q, R>, I, A, A>, ) -> <ForgetBrand<Q, R> as Kind_266801a817966495>::Of<'a, S, S>

§Type Signature

forall PointerBrand I S T A B F R Q. (Monoid R, ToDynCloneFn Q, IndexedFoldFunc F, ToDynCloneFn PointerBrand) => (&IndexedFold PointerBrand I S T A B F, Indexed (Forget Q R) I A A) -> Forget Q R S S

§Type Parameters
  • R: The monoid type.
  • Q: The reference-counted pointer type.
§Parameters
  • &self: The indexed fold instance.
  • pab: The indexed profunctor value to transform.
§Returns

The transformed profunctor value.

§Examples
use fp_library::{
	brands::{
		optics::*,
		*,
	},
	classes::{
		IndexedFoldOptic,
		ToDynCloneFn,
		monoid::Monoid,
		semigroup::Semigroup,
	},
	types::optics::*,
};
#[derive(Clone)]
struct MyFold;
impl<'a> IndexedFoldFunc<'a, usize, Vec<i32>, i32> for MyFold {
	fn apply<R: 'a + Monoid + 'static>(
		&self,
		f: Box<dyn Fn(usize, i32) -> R + 'a>,
		s: Vec<i32>,
	) -> R {
		s.into_iter()
			.enumerate()
			.fold(R::empty(), |acc, (i, x)| Semigroup::append(acc, f(i, x)))
	}
}
let l: IndexedFold<RcBrand, usize, Vec<i32>, Vec<i32>, i32, i32, MyFold> =
	IndexedFold::new(MyFold);
let f = Forget::<RcBrand, String, (usize, i32), i32>::new(|(i, x)| format!("[{}]={}", i, x));
let pab = Indexed::new(f);
let result = IndexedFoldOptic::evaluate::<String, RcBrand>(&l, pab);
assert_eq!(result.run(vec![10, 20]), "[0]=10[1]=20");
Source§

impl<'a, Q2: ToDynCloneFn + 'static, R: 'a + Monoid + Clone + 'static, PointerBrand: ToDynCloneFn, I: 'a, S: 'a, T: 'a, A: 'a, B: 'a, F> IndexedOpticAdapter<'a, ForgetBrand<Q2, R>, I, S, S, A, A> for IndexedFold<'a, PointerBrand, I, S, T, A, B, F>
where F: IndexedFoldFunc<'a, I, S, A> + Clone + 'a,

§Type Parameters
  • 'a: The lifetime of the values.
  • Q2: The result type brand.
  • R: The monoid type.
  • PointerBrand: The original profunctor type.
  • I: The index type.
  • S: The source type.
  • T: The target type.
  • A: The source focus type.
  • B: The target focus type.
  • F: The fold function type.
Source§

fn evaluate_indexed( &self, pab: Indexed<'a, ForgetBrand<Q2, R>, I, A, A>, ) -> Forget<'a, Q2, R, S, S>

§Type Signature

forall Q2 R PointerBrand I S T A B F. (ToDynCloneFn Q2, Monoid R, ToDynCloneFn PointerBrand, IndexedFoldFunc F) => (&IndexedFold Q2 R PointerBrand I S T A B F, Indexed (Forget Q2 R) I A A) -> Forget Q2 R S S

§Parameters
  • &self: The indexed fold instance.
  • pab: The indexed profunctor value.
§Returns

The transformed profunctor value.

§Examples
use fp_library::{
	brands::{
		RcBrand,
		VecBrand,
		optics::*,
	},
	functions::*,
	types::optics::{
		Folded,
		*,
	},
};
let l = IndexedFold::<RcBrand, usize, Vec<i32>, Vec<i32>, i32, i32, Folded<VecBrand>>::folded();
let _unindexed = optics_un_index::<ForgetBrand<RcBrand, String>, _, _, _, _, _>(&l);
assert_eq!(
	optics_indexed_fold_map::<RcBrand, _, _, _, String>(&l, |_, x| x.to_string(), vec![1, 2]),
	"12"
);
Source§

impl<'a, Q2: ToDynCloneFn + 'static, R: 'a + Monoid + Clone + 'static, PointerBrand: ToDynCloneFn, I: 'a, S: 'a, T: 'a, A: 'a, B: 'a, F> IndexedOpticAdapterDiscardsFocus<'a, ForgetBrand<Q2, R>, I, S, S, A, A> for IndexedFold<'a, PointerBrand, I, S, T, A, B, F>
where F: IndexedFoldFunc<'a, I, S, A> + Clone + 'a,

§Type Parameters
  • 'a: The lifetime of the values.
  • Q2: The result type brand.
  • R: The monoid type.
  • PointerBrand: The original profunctor type.
  • I: The index type.
  • S: The source type.
  • T: The target type.
  • A: The source focus type.
  • B: The target focus type.
  • F: The fold function type.
Source§

fn evaluate_indexed_discards_focus( &self, pab: Indexed<'a, ForgetBrand<Q2, R>, I, A, A>, ) -> Forget<'a, Q2, R, S, S>

§Type Signature

forall Q2 R PointerBrand I S T A B F. (ToDynCloneFn Q2, Monoid R, ToDynCloneFn PointerBrand, IndexedFoldFunc F) => (&IndexedFold Q2 R PointerBrand I S T A B F, Indexed (Forget Q2 R) I A A) -> Forget Q2 R S S

§Parameters
  • &self: The indexed fold instance.
  • pab: The indexed profunctor value.
§Returns

The transformed profunctor value.

§Examples
use fp_library::{
	brands::{
		RcBrand,
		VecBrand,
		optics::*,
	},
	functions::*,
	types::optics::{
		Folded,
		*,
	},
};
let l = IndexedFold::<RcBrand, usize, Vec<i32>, Vec<i32>, i32, i32, Folded<VecBrand>>::folded();
let _unindexed = optics_as_index::<ForgetBrand<RcBrand, String>, _, _, _, _, _>(&l);
assert_eq!(
	optics_indexed_fold_map::<RcBrand, _, _, _, String>(&l, |i, _| i.to_string(), vec![1, 2]),
	"01"
);

Auto Trait Implementations§

§

impl<'a, PointerBrand, I, S, T, A, B, F> Freeze for IndexedFold<'a, PointerBrand, I, S, T, A, B, F>
where F: Freeze,

§

impl<'a, PointerBrand, I, S, T, A, B, F> RefUnwindSafe for IndexedFold<'a, PointerBrand, I, S, T, A, B, F>

§

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

§

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

§

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

§

impl<'a, PointerBrand, I, S, T, A, B, F> UnsafeUnpin for IndexedFold<'a, PointerBrand, I, S, T, A, B, F>
where F: UnsafeUnpin,

§

impl<'a, PointerBrand, I, S, T, A, B, F> UnwindSafe for IndexedFold<'a, PointerBrand, I, 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.