pub struct Pair<First, Second>(pub First, pub Second);Expand description
Wraps two values.
A simple tuple struct that holds two values of potentially different types.
§Higher-Kinded Type Representation
This type has multiple higher-kinded representations:
PairBrand: fully polymorphic over both values (bifunctor).PairFirstAppliedBrand<First>: the first value type is fixed, polymorphic over the second (functor over second).PairSecondAppliedBrand<Second>: the second value type is fixed, polymorphic over the first (functor over first).
§Serialization
This type supports serialization and deserialization via serde when the serde feature is enabled.
§Type Parameters
First: The type of the first value.Second: The type of the second value.
§Fields
0: The first value.1: The second value.
Tuple Fields§
§0: First§1: SecondImplementations§
Source§impl<First, Second> Pair<First, Second>
§Type Parameters
First: The type of the first value.
Second: The type of the second value.
impl<First, Second> Pair<First, Second>
§Type Parameters
First: The type of the first value.Second: The type of the second value.
Sourcepub fn bimap<B, D>(
self,
f: impl FnOnce(First) -> B,
g: impl FnOnce(Second) -> D,
) -> Pair<B, D>
pub fn bimap<B, D>( self, f: impl FnOnce(First) -> B, g: impl FnOnce(Second) -> D, ) -> Pair<B, D>
Maps functions over both values in the pair.
See Bifunctor::bimap for the type class version.
§Type Signature
forall First Second B D. (Pair First Second, First -> B, Second -> D) -> Pair B D
§Type Parameters
B: The type of the mapped first value.D: The type of the mapped second value.
§Parameters
self: The pair instance.f: The function to apply to the first value.g: The function to apply to the second value.
§Returns
A new pair containing the mapped values.
§Examples
use fp_library::types::*;
let x = Pair(1, 5);
assert_eq!(x.bimap(|a| a + 1, |b| b * 2), Pair(2, 10));Sourcepub fn map_first<B>(self, f: impl FnOnce(First) -> B) -> Pair<B, Second>
pub fn map_first<B>(self, f: impl FnOnce(First) -> B) -> Pair<B, Second>
Maps a function over the first value in the pair.
See Bifunctor::bimap for the type class version.
§Type Signature
forall First Second B. (Pair First Second, First -> B) -> Pair B Second
§Type Parameters
B: The type of the mapped first value.
§Parameters
self: The pair instance.f: The function to apply to the first value.
§Returns
A new pair with the transformed first value.
§Examples
use fp_library::types::*;
let x = Pair(1, 5);
assert_eq!(x.map_first(|a| a + 1), Pair(2, 5));Sourcepub fn map_second<D>(self, g: impl FnOnce(Second) -> D) -> Pair<First, D>
pub fn map_second<D>(self, g: impl FnOnce(Second) -> D) -> Pair<First, D>
Maps a function over the second value in the pair.
See Bifunctor::bimap for the type class version.
§Type Signature
forall First Second D. (Pair First Second, Second -> D) -> Pair First D
§Type Parameters
D: The type of the mapped second value.
§Parameters
self: The pair instance.g: The function to apply to the second value.
§Returns
A new pair with the transformed second value.
§Examples
use fp_library::types::*;
let x = Pair(1, 5);
assert_eq!(x.map_second(|b| b * 2), Pair(1, 10));Sourcepub fn fold<C>(
self,
f: impl FnOnce(First) -> C,
g: impl FnOnce(Second) -> C,
combine: impl FnOnce(C, C) -> C,
) -> C
pub fn fold<C>( self, f: impl FnOnce(First) -> C, g: impl FnOnce(Second) -> C, combine: impl FnOnce(C, C) -> C, ) -> C
Folds both values into a single result.
Applies two functions to the first and second values respectively,
then combines the results using FnOnce.
§Type Signature
forall First Second C. (Pair First Second, First -> C, Second -> C, (C, C) -> C) -> C
§Type Parameters
C: The result type.
§Parameters
self: The pair instance.f: The function to apply to the first value.g: The function to apply to the second value.combine: The function to combine the results.
§Returns
The combined result.
§Examples
use fp_library::types::*;
let x = Pair(1, 2);
let y = x.fold(|a| a.to_string(), |b| b.to_string(), |a, b| format!("{a},{b}"));
assert_eq!(y, "1,2");Sourcepub fn bi_fold_right<C>(
self,
f: impl FnOnce(First, C) -> C,
g: impl FnOnce(Second, C) -> C,
z: C,
) -> C
pub fn bi_fold_right<C>( self, f: impl FnOnce(First, C) -> C, g: impl FnOnce(Second, C) -> C, z: C, ) -> C
Folds the pair from right to left using two step functions.
See Bifoldable::bi_fold_right for the type class version.
§Type Signature
forall First Second C. (Pair First Second, (First, C) -> C, (Second, C) -> C, C) -> C
§Type Parameters
C: The accumulator type.
§Parameters
self: The pair instance.f: The step function for the first value.g: The step function for the second value.z: The initial accumulator.
§Returns
The result of folding: f(first, g(second, z)).
§Examples
use fp_library::types::*;
let x = Pair(3, 5);
assert_eq!(x.bi_fold_right(|a, acc| acc - a, |b, acc| acc + b, 0), 2);Sourcepub fn bi_fold_left<C>(
self,
f: impl FnOnce(C, First) -> C,
g: impl FnOnce(C, Second) -> C,
z: C,
) -> C
pub fn bi_fold_left<C>( self, f: impl FnOnce(C, First) -> C, g: impl FnOnce(C, Second) -> C, z: C, ) -> C
Folds the pair from left to right using two step functions.
See Bifoldable::bi_fold_left for the type class version.
§Type Signature
forall First Second C. (Pair First Second, (C, First) -> C, (C, Second) -> C, C) -> C
§Type Parameters
C: The accumulator type.
§Parameters
self: The pair instance.f: The step function for the first value.g: The step function for the second value.z: The initial accumulator.
§Returns
The result of folding: g(f(z, first), second).
§Examples
use fp_library::types::*;
let x = Pair(3, 5);
assert_eq!(x.bi_fold_left(|acc, a| acc - a, |acc, b| acc + b, 0), 2);Sourcepub fn bi_fold_map<M: Semigroup>(
self,
f: impl FnOnce(First) -> M,
g: impl FnOnce(Second) -> M,
) -> M
pub fn bi_fold_map<M: Semigroup>( self, f: impl FnOnce(First) -> M, g: impl FnOnce(Second) -> M, ) -> M
Maps both values to a monoid and combines the results.
See Bifoldable::bi_fold_map for the type class version.
§Type Signature
forall First Second M. Semigroup M => (Pair First Second, First -> M, Second -> M) -> M
§Type Parameters
M: The monoid type.
§Parameters
self: The pair instance.f: The function mapping the first value to the monoid.g: The function mapping the second value to the monoid.
§Returns
The combined monoid value.
§Examples
use fp_library::types::*;
let x = Pair(3, 5);
assert_eq!(x.bi_fold_map(|a: i32| a.to_string(), |b: i32| b.to_string()), "35".to_string());Source§impl<'a, First: 'a, Second: 'a> Pair<First, Second>
§Type Parameters
First: The type of the first value.
Second: The type of the second value.
impl<'a, First: 'a, Second: 'a> Pair<First, Second>
§Type Parameters
First: The type of the first value.Second: The type of the second value.
Sourcepub fn bi_traverse<C: 'a + Clone, D: 'a + Clone, F: Applicative>(
self,
f: impl Fn(First) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, C> + 'a,
g: impl Fn(Second) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, D> + 'a,
) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, Pair<C, D>>
pub fn bi_traverse<C: 'a + Clone, D: 'a + Clone, F: Applicative>( self, f: impl Fn(First) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, C> + 'a, g: impl Fn(Second) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, D> + 'a, ) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, Pair<C, D>>
Traverses the pair with two effectful functions.
See Bitraversable::bi_traverse for the type class version.
§Type Signature
forall First Second C D F. Applicative F => (Pair First Second, First -> F C, Second -> F D) -> F (Pair C D)
§Type Parameters
C: The output type for the first value.D: The output type for the second value.F: The applicative context.
§Parameters
self: The pair instance.f: The function for the first value.g: The function for the second value.
§Returns
A pair of the transformed values wrapped in the applicative context.
§Examples
use fp_library::{
brands::*,
types::*,
};
let x = Pair(3, 5);
let y = x.bi_traverse::<_, _, OptionBrand>(|a| Some(a + 1), |b| Some(b * 2));
assert_eq!(y, Some(Pair(4, 10)));Source§impl<First: Semigroup, Second> Pair<First, Second>
§Type Parameters
First: The type of the first value.
Second: The type of the second value.
impl<First: Semigroup, Second> Pair<First, Second>
§Type Parameters
First: The type of the first value.Second: The type of the second value.
Sourcepub fn bind<C>(self, f: impl FnOnce(Second) -> Pair<First, C>) -> Pair<First, C>
pub fn bind<C>(self, f: impl FnOnce(Second) -> Pair<First, C>) -> Pair<First, C>
Chains a computation over the second value, combining first values via their semigroup.
See Semimonad::bind for the type class version
(via PairFirstAppliedBrand).
§Type Signature
forall First Second C. Semigroup First => (Pair First Second, Second -> Pair First C) -> Pair First C
§Type Parameters
C: The type of the new second value.
§Parameters
self: The pair instance.f: The function to apply to the second value.
§Returns
A new pair where the first values are combined and the second value is transformed.
§Examples
use fp_library::types::*;
assert_eq!(
Pair("a".to_string(), 5).bind(|x| Pair("b".to_string(), x * 2)),
Pair("ab".to_string(), 10)
);Source§impl<First, Second: Semigroup> Pair<First, Second>
§Type Parameters
First: The type of the first value.
Second: The type of the second value.
impl<First, Second: Semigroup> Pair<First, Second>
§Type Parameters
First: The type of the first value.Second: The type of the second value.
Sourcepub fn bind_first<C>(
self,
f: impl FnOnce(First) -> Pair<C, Second>,
) -> Pair<C, Second>
pub fn bind_first<C>( self, f: impl FnOnce(First) -> Pair<C, Second>, ) -> Pair<C, Second>
Chains a computation over the first value, combining second values via their semigroup.
See Semimonad::bind for the type class version
(via PairSecondAppliedBrand).
§Type Signature
forall First Second C. Semigroup Second => (Pair First Second, First -> Pair C Second) -> Pair C Second
§Type Parameters
C: The type of the new first value.
§Parameters
self: The pair instance.f: The function to apply to the first value.
§Returns
A new pair where the first value is transformed and the second values are combined.
§Examples
use fp_library::types::*;
assert_eq!(
Pair(5, "a".to_string()).bind_first(|x| Pair(x * 2, "b".to_string())),
Pair(10, "ab".to_string())
);Trait Implementations§
Source§impl<'de, First, Second> Deserialize<'de> for Pair<First, Second>where
First: Deserialize<'de>,
Second: Deserialize<'de>,
impl<'de, First, Second> Deserialize<'de> for Pair<First, Second>where
First: Deserialize<'de>,
Second: 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<First: Ord, Second: Ord> Ord for Pair<First, Second>
impl<First: Ord, Second: Ord> Ord for Pair<First, Second>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<First: PartialOrd, Second: PartialOrd> PartialOrd for Pair<First, Second>
impl<First: PartialOrd, Second: PartialOrd> PartialOrd for Pair<First, Second>
impl<First: Copy, Second: Copy> Copy for Pair<First, Second>
impl<First: Eq, Second: Eq> Eq for Pair<First, Second>
impl<First, Second> StructuralPartialEq for Pair<First, Second>
Auto Trait Implementations§
impl<First, Second> Freeze for Pair<First, Second>
impl<First, Second> RefUnwindSafe for Pair<First, Second>where
First: RefUnwindSafe,
Second: RefUnwindSafe,
impl<First, Second> Send for Pair<First, Second>
impl<First, Second> Sync for Pair<First, Second>
impl<First, Second> Unpin for Pair<First, Second>
impl<First, Second> UnsafeUnpin for Pair<First, Second>where
First: UnsafeUnpin,
Second: UnsafeUnpin,
impl<First, Second> UnwindSafe for Pair<First, Second>where
First: UnwindSafe,
Second: 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