pub struct Identity<A>(pub A);Expand description
Wraps a value.
The Identity type represents a trivial wrapper around a value. It is the simplest possible container.
It is often used as a base case for higher-kinded types or when a container is required but no additional effect is needed.
§Higher-Kinded Type Representation
The higher-kinded representation of this type constructor is IdentityBrand,
which is fully polymorphic over the wrapped value type.
§Serialization
This type supports serialization and deserialization via serde when the serde feature is enabled.
§Type Parameters
A: The type of the wrapped value.
§Fields
0: The wrapped value.
Tuple Fields§
§0: AImplementations§
Source§impl<A> Identity<A>
§Type Parameters
'a: The lifetime of the values.
A: The type of the wrapped value.
impl<A> Identity<A>
§Type Parameters
'a: The lifetime of the values.A: The type of the wrapped value.
Sourcepub fn map<B>(self, f: impl FnOnce(A) -> B) -> Identity<B>
pub fn map<B>(self, f: impl FnOnce(A) -> B) -> Identity<B>
Maps a function over the value in the identity.
This is the inherent version of Functor::map, accepting
FnOnce instead of Fn since it consumes self.
§Type Signature
forall A B. (Identity A, A -> B) -> Identity B
§Type Parameters
B: The type of the result of applying the function.
§Parameters
self: The identity instance.f: The function to apply.
§Returns
A new identity containing the result of applying the function.
§Examples
use fp_library::types::*;
let x = Identity(5);
let y = x.map(|i| i * 2);
assert_eq!(y, Identity(10));Sourcepub fn lift2<B, C>(
self,
other: Identity<B>,
f: impl FnOnce(A, B) -> C,
) -> Identity<C>
pub fn lift2<B, C>( self, other: Identity<B>, f: impl FnOnce(A, B) -> C, ) -> Identity<C>
Lifts a binary function to operate on two identities.
See Lift::lift2 for the type class version.
§Type Signature
forall A B C. (Identity A, Identity B, (A, B) -> C) -> Identity C
§Type Parameters
B: The type of the other identity’s value.C: The return type of the function.
§Parameters
self: The identity instance.other: The other identity.f: The binary function to apply.
§Returns
A new identity containing the result of applying the function.
§Examples
use fp_library::types::*;
let x = Identity(1);
let y = Identity(2);
let z = x.lift2(y, |a, b| a + b);
assert_eq!(z, Identity(3));Sourcepub fn apply<B>(self, ff: Identity<impl FnOnce(A) -> B>) -> Identity<B>
pub fn apply<B>(self, ff: Identity<impl FnOnce(A) -> B>) -> Identity<B>
Applies a wrapped function to a value.
See Semiapplicative::apply for the type class version.
§Type Signature
forall A B. (Identity A, Identity (A -> B)) -> Identity B
§Type Parameters
B: The return type of the wrapped function.
§Parameters
self: The identity instance.ff: The identity containing the function.
§Returns
A new identity containing the result.
§Examples
use fp_library::types::*;
let f = Identity(|x: i32| x * 2);
let x = Identity(5);
let y = x.apply(f);
assert_eq!(y, Identity(10));Sourcepub fn bind<B>(self, f: impl FnOnce(A) -> Identity<B>) -> Identity<B>
pub fn bind<B>(self, f: impl FnOnce(A) -> Identity<B>) -> Identity<B>
Chains identity computations.
This is the inherent version of Semimonad::bind, accepting
FnOnce instead of Fn since it consumes self.
§Type Signature
forall A B. (Identity A, A -> Identity B) -> Identity B
§Type Parameters
B: The type of the result of the chained computation.
§Parameters
self: The identity instance.f: The function to apply to the value inside the identity.
§Returns
The result of applying f to the value.
§Examples
use fp_library::types::*;
let x = Identity(5);
let y = x.bind(|i| Identity(i * 2));
assert_eq!(y, Identity(10));Sourcepub fn fold_right<B>(self, f: impl FnOnce(A, B) -> B, initial: B) -> B
pub fn fold_right<B>(self, f: impl FnOnce(A, B) -> B, initial: B) -> B
Folds the identity from the right.
See Foldable::fold_right for the type class version.
§Type Signature
forall A B. (Identity A, (A, B) -> B, B) -> B
§Type Parameters
B: The type of the accumulator.
§Parameters
self: The identity instance.f: The function to apply to the element and the accumulator.initial: The initial value of the accumulator.
§Returns
The final accumulator value.
§Examples
use fp_library::types::*;
let x = Identity(5);
let y = x.fold_right(|a, b| a + b, 10);
assert_eq!(y, 15);Sourcepub fn fold_left<B>(self, f: impl FnOnce(B, A) -> B, initial: B) -> B
pub fn fold_left<B>(self, f: impl FnOnce(B, A) -> B, initial: B) -> B
Folds the identity from the left.
See Foldable::fold_left for the type class version.
§Type Signature
forall A B. (Identity A, (B, A) -> B, B) -> B
§Type Parameters
B: The type of the accumulator.
§Parameters
self: The identity instance.f: The function to apply to the accumulator and the element.initial: The initial value of the accumulator.
§Returns
The final accumulator value.
§Examples
use fp_library::types::*;
let x = Identity(5);
let y = x.fold_left(|b, a| b + a, 10);
assert_eq!(y, 15);Sourcepub fn fold_map<M>(self, f: impl FnOnce(A) -> M) -> M
pub fn fold_map<M>(self, f: impl FnOnce(A) -> M) -> M
Maps the value to a monoid and returns it.
See Foldable::fold_map for the type class version.
§Type Signature
forall A M. (Identity A, A -> M) -> M
§Type Parameters
M: The monoid type.
§Parameters
self: The identity instance.f: The mapping function.
§Returns
The monoid value.
§Examples
use fp_library::types::*;
let x = Identity(5);
let y = x.fold_map(|a: i32| a.to_string());
assert_eq!(y, "5".to_string());Source§impl<'a, A: 'a> Identity<A>
§Type Parameters
'a: The lifetime of the values.
A: The type of the wrapped value.
impl<'a, A: 'a> Identity<A>
§Type Parameters
'a: The lifetime of the values.A: The type of the wrapped value.
Sourcepub fn traverse<B: 'a + Clone, F: Applicative>(
self,
f: impl Fn(A) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, B> + 'a,
) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, Identity<B>>
pub fn traverse<B: 'a + Clone, F: Applicative>( self, f: impl Fn(A) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, B> + 'a, ) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, Identity<B>>
Traverses the identity with an applicative function.
See Traversable::traverse for the type class version.
§Type Signature
forall A B F. Applicative F => (Identity A, A -> F B) -> F (Identity B)
§Type Parameters
B: The type of the elements in the resulting identity.F: The applicative context.
§Parameters
self: The identity instance.f: The function to apply, returning a value in an applicative context.
§Returns
The identity wrapped in the applicative context.
§Examples
use fp_library::{
brands::*,
types::*,
};
let x = Identity(5);
let y = x.traverse::<_, OptionBrand>(|a| Some(a * 2));
assert_eq!(y, Some(Identity(10)));Sourcepub fn sequence<InnerA: 'a + Clone, F: Applicative>(
self,
) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, Identity<InnerA>>
pub fn sequence<InnerA: 'a + Clone, F: Applicative>( self, ) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, Identity<InnerA>>
Sequences an identity containing an applicative value.
See Traversable::sequence for the type class version.
§Type Signature
forall A InnerA F. (Applicative F, Into A) => Identity A -> F (Identity InnerA)
§Type Parameters
InnerA: The inner type wrapped in the applicative context.F: The applicative context.
§Returns
The identity wrapped in the applicative context.
§Examples
use fp_library::{
brands::*,
types::*,
};
let x = Identity(Some(5));
let y: Option<Identity<i32>> = x.sequence::<i32, OptionBrand>();
assert_eq!(y, Some(Identity(5)));Trait Implementations§
Source§impl<'de, A> Deserialize<'de> for Identity<A>where
A: Deserialize<'de>,
impl<'de, A> Deserialize<'de> for Identity<A>where
A: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<A: Ord> Ord for Identity<A>
impl<A: Ord> Ord for Identity<A>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<A: PartialOrd> PartialOrd for Identity<A>
impl<A: PartialOrd> PartialOrd for Identity<A>
impl<A: Copy> Copy for Identity<A>
impl<A: Eq> Eq for Identity<A>
impl<A> StructuralPartialEq for Identity<A>
Auto Trait Implementations§
impl<A> Freeze for Identity<A>where
A: Freeze,
impl<A> RefUnwindSafe for Identity<A>where
A: RefUnwindSafe,
impl<A> Send for Identity<A>where
A: Send,
impl<A> Sync for Identity<A>where
A: Sync,
impl<A> Unpin for Identity<A>where
A: Unpin,
impl<A> UnsafeUnpin for Identity<A>where
A: UnsafeUnpin,
impl<A> UnwindSafe for Identity<A>where
A: UnwindSafe,
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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