Skip to main content

Pair

Struct Pair 

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

§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: Second

Implementations§

Source§

impl<First, Second> Pair<First, Second>

§Type Parameters
  • First: The type of the first value.
  • Second: The type of the second value.
Source

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));
Source

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));
Source

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));
Source

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");
Source

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);
Source

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);
Source

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

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>>
where Pair<C, D>: Clone,

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

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

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<First: Clone, Second: Clone> Clone for Pair<First, Second>

Source§

fn clone(&self) -> Pair<First, Second>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<First: Debug, Second: Debug> Debug for Pair<First, Second>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<First: Default, Second: Default> Default for Pair<First, Second>

Source§

fn default() -> Pair<First, Second>

Returns the “default value” for a type. Read more
Source§

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

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<First: Hash, Second: Hash> Hash for Pair<First, Second>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<First: Ord, Second: Ord> Ord for Pair<First, Second>

Source§

fn cmp(&self, other: &Pair<First, Second>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<First: PartialEq, Second: PartialEq> PartialEq for Pair<First, Second>

Source§

fn eq(&self, other: &Pair<First, Second>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<First: PartialOrd, Second: PartialOrd> PartialOrd for Pair<First, Second>

Source§

fn partial_cmp(&self, other: &Pair<First, Second>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<First, Second> Serialize for Pair<First, Second>
where First: Serialize, Second: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<First: Copy, Second: Copy> Copy for Pair<First, Second>

Source§

impl<First: Eq, Second: Eq> Eq for Pair<First, Second>

Source§

impl<First, Second> StructuralPartialEq for Pair<First, Second>

Auto Trait Implementations§

§

impl<First, Second> Freeze for Pair<First, Second>
where First: Freeze, Second: Freeze,

§

impl<First, Second> RefUnwindSafe for Pair<First, Second>
where First: RefUnwindSafe, Second: RefUnwindSafe,

§

impl<First, Second> Send for Pair<First, Second>
where First: Send, Second: Send,

§

impl<First, Second> Sync for Pair<First, Second>
where First: Sync, Second: Sync,

§

impl<First, Second> Unpin for Pair<First, Second>
where First: Unpin, Second: Unpin,

§

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

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,