Skip to main content

ThunkBrand

Struct ThunkBrand 

Source
pub struct ThunkBrand;
Expand description

Brand for Thunk.

Note: This is for Thunk<'a, A>, NOT for Trampoline<A>. Trampoline cannot implement HKT traits due to its 'static requirement.

Trait Implementations§

Source§

impl ApplyFirst for ThunkBrand

Source§

fn apply_first<'a, A: 'a + Clone, B: 'a + Clone>( fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, fb: <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>

Combines two contexts, keeping the value from the first context. Read more
Source§

impl ApplySecond for ThunkBrand

Source§

fn apply_second<'a, A: 'a + Clone, B: 'a + Clone>( fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, fb: <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>

Combines two contexts, keeping the value from the second context. Read more
Source§

impl Clone for ThunkBrand

Source§

fn clone(&self) -> ThunkBrand

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 Debug for ThunkBrand

Source§

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

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

impl Default for ThunkBrand

Source§

fn default() -> ThunkBrand

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

impl Evaluable for ThunkBrand

Source§

fn evaluate<'a, A: 'a>(fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>) -> A

Runs the eval, producing the inner value.

§Type Signature

forall self a. Evaluable self => self a -> a

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the value inside the thunk.
§Parameters
  • fa: The eval to run.
§Returns

The result of running the thunk.

§Examples
use fp_library::{brands::*, classes::*, functions::*, types::*};

let thunk = Thunk::new(|| 42);
assert_eq!(evaluate::<ThunkBrand, _>(thunk), 42);
Source§

impl Foldable for ThunkBrand

Source§

fn fold_right<'a, FnBrand, A: 'a, B: 'a, Func>( func: Func, initial: B, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> B
where Func: Fn(A, B) -> B + 'a, FnBrand: CloneableFn + 'a,

Folds the Thunk from the right.

§Type Signature

forall self a b. Foldable self => ((a, b) -> b, b, self a) -> b

§Type Parameters
  • 'a: The lifetime of the computation.
  • FnBrand: The brand of the cloneable function to use.
  • A: The type of the elements in the structure.
  • B: The type of the accumulator.
  • Func: The type of the folding function.
§Parameters
  • func: The function to apply to each element and the accumulator.
  • initial: The initial value of the accumulator.
  • fa: The Thunk to fold.
§Returns

The final accumulator value.

§Examples
use fp_library::{brands::*, functions::*};

let thunk = pure::<ThunkBrand, _>(10);
let result = fold_right::<RcFnBrand, ThunkBrand, _, _, _>(|a, b| a + b, 5, thunk);
assert_eq!(result, 15);
Source§

fn fold_left<'a, FnBrand, A: 'a, B: 'a, Func>( func: Func, initial: B, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> B
where Func: Fn(B, A) -> B + 'a, FnBrand: CloneableFn + 'a,

Folds the Thunk from the left.

§Type Signature

forall self a b. Foldable self => ((b, a) -> b, b, self a) -> b

§Type Parameters
  • 'a: The lifetime of the computation.
  • FnBrand: The brand of the cloneable function to use.
  • A: The type of the elements in the structure.
  • B: The type of the accumulator.
  • Func: The type of the folding function.
§Parameters
  • func: The function to apply to the accumulator and each element.
  • initial: The initial value of the accumulator.
  • fa: The Thunk to fold.
§Returns

The final accumulator value.

§Examples
use fp_library::{brands::*, functions::*};

let thunk = pure::<ThunkBrand, _>(10);
let result = fold_left::<RcFnBrand, ThunkBrand, _, _, _>(|b, a| b + a, 5, thunk);
assert_eq!(result, 15);
Source§

fn fold_map<'a, FnBrand, A: 'a, M, Func>( func: Func, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> M
where M: Monoid + 'a, Func: Fn(A) -> M + 'a, FnBrand: CloneableFn + 'a,

Maps the value to a monoid and returns it.

§Type Signature

forall self a m. (Foldable self, Monoid m) => (a -> m, self a) -> m

§Type Parameters
  • 'a: The lifetime of the computation.
  • FnBrand: The brand of the cloneable function to use.
  • A: The type of the elements in the structure.
  • M: The type of the monoid.
  • Func: The type of the mapping function.
§Parameters
  • func: The mapping function.
  • fa: The Thunk to fold.
§Returns

The monoid value.

§Examples
use fp_library::{brands::*, functions::*};

let thunk = pure::<ThunkBrand, _>(10);
let result = fold_map::<RcFnBrand, ThunkBrand, _, _, _>(|a| a.to_string(), thunk);
assert_eq!(result, "10");
Source§

impl Functor for ThunkBrand

Source§

fn map<'a, A: 'a, B: 'a, Func>( func: Func, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
where Func: Fn(A) -> B + 'a,

Maps a function over the result of a Thunk computation.

§Type Signature

forall self a b. Functor self => (a -> b, self a) -> self b

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the value inside the Thunk.
  • B: The type of the result of the transformation.
  • Func: The type of the transformation function.
§Parameters
  • func: The function to apply to the result of the computation.
  • fa: The Thunk instance.
§Returns

A new Thunk instance with the transformed result.

§Examples
use fp_library::{brands::*, functions::*};

let thunk = pure::<ThunkBrand, _>(10);
let mapped = map::<ThunkBrand, _, _, _>(|x| x * 2, thunk);
assert_eq!(mapped.evaluate(), 20);
Source§

impl Hash for ThunkBrand

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 Kind_cdc7cd43dac7585f for ThunkBrand

Generated implementation of Kind_cdc7cd43dac7585f for ThunkBrand.

Source§

type Of<'a, A: 'a> = Thunk<'a, A>

The applied type.
Source§

impl Lift for ThunkBrand

Source§

fn lift2<'a, A, B, C, Func>( func: Func, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, fb: <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, C>
where Func: Fn(A, B) -> C + 'a, A: Clone + 'a, B: Clone + 'a, C: 'a,

Lifts a binary function into the Thunk context.

§Type Signature

forall self a b c. Lift self => ((a, b) -> c, self a, self b) -> self c

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the first value.
  • B: The type of the second value.
  • C: The type of the result.
  • Func: The type of the binary function.
§Parameters
  • func: The binary function to apply.
  • fa: The first Thunk.
  • fb: The second Thunk.
§Returns

A new Thunk instance containing the result of applying the function.

§Examples
use fp_library::{brands::*, functions::*};

let eval1 = pure::<ThunkBrand, _>(10);
let eval2 = pure::<ThunkBrand, _>(20);
let result = lift2::<ThunkBrand, _, _, _, _>(|a, b| a + b, eval1, eval2);
assert_eq!(result.evaluate(), 30);
Source§

impl MonadRec for ThunkBrand

Source§

fn tail_rec_m<'a, A: 'a, B: 'a, F>( f: F, a: A, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
where F: Fn(A) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, Step<A, B>> + Clone + 'a,

Performs tail-recursive monadic computation.

§Type Signature

forall self a b. MonadRec self => (a -> self (Step a b), a) -> self b

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the initial value and loop state.
  • B: The type of the result.
  • F: The type of the step function.
§Parameters
  • f: The step function.
  • a: The initial value.
§Returns

The result of the computation.

§Examples
use fp_library::{brands::*, classes::*, functions::*, types::*};

let result = tail_rec_m::<ThunkBrand, _, _, _>(
    |x| pure::<ThunkBrand, _>(if x < 1000 { Step::Loop(x + 1) } else { Step::Done(x) }),
    0,
);
assert_eq!(result.evaluate(), 1000);
Source§

impl Ord for ThunkBrand

Source§

fn cmp(&self, other: &ThunkBrand) -> 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 PartialEq for ThunkBrand

Source§

fn eq(&self, other: &ThunkBrand) -> 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 PartialOrd for ThunkBrand

Source§

fn partial_cmp(&self, other: &ThunkBrand) -> 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 Pointed for ThunkBrand

Source§

fn pure<'a, A: 'a>(a: A) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>

Wraps a value in a Thunk context.

§Type Signature

forall self a. Pointed self => a -> self a

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the value to wrap.
§Parameters
  • a: The value to wrap.
§Returns

A new Thunk instance containing the value.

§Examples
use fp_library::{brands::*, functions::*, types::*};

let thunk: Thunk<i32> = pure::<ThunkBrand, _>(42);
assert_eq!(thunk.evaluate(), 42);
Source§

impl Semiapplicative for ThunkBrand

Source§

fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>( ff: <Self as Kind_cdc7cd43dac7585f>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>

Applies a function wrapped in Thunk to a value wrapped in Thunk.

§Type Signature

forall self a b. Semiapplicative self => (self (a -> b), self a) -> self b

§Type Parameters
  • 'a: The lifetime of the computation.
  • FnBrand: The brand of the cloneable function wrapper.
  • A: The type of the input.
  • B: The type of the result.
§Parameters
  • ff: The Thunk containing the function.
  • fa: The Thunk containing the value.
§Returns

A new Thunk instance containing the result of applying the function.

§Examples
use fp_library::{brands::*, functions::*};

let func = pure::<ThunkBrand, _>(cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
let val = pure::<ThunkBrand, _>(21);
let result = apply::<RcFnBrand, ThunkBrand, _, _>(func, val);
assert_eq!(result.evaluate(), 42);
Source§

impl Semimonad for ThunkBrand

Source§

fn bind<'a, A: 'a, B: 'a, Func>( ma: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, func: Func, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
where Func: Fn(A) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B> + 'a,

Chains Thunk computations.

§Type Signature

forall self a b. Semimonad self => (self a, a -> self b) -> self b

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the result of the first computation.
  • B: The type of the result of the new computation.
  • Func: The type of the function to apply.
§Parameters
  • ma: The first Thunk.
  • func: The function to apply to the result of the computation.
§Returns

A new Thunk instance representing the chained computation.

§Examples
use fp_library::{brands::*, functions::*};

let thunk = pure::<ThunkBrand, _>(10);
let result = bind::<ThunkBrand, _, _, _>(thunk, |x| pure::<ThunkBrand, _>(x * 2));
assert_eq!(result.evaluate(), 20);
Source§

impl Copy for ThunkBrand

Source§

impl Eq for ThunkBrand

Source§

impl StructuralPartialEq for ThunkBrand

Auto Trait Implementations§

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> 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<Brand> Applicative for Brand

Source§

impl<Brand> Monad for Brand
where Brand: Applicative + Semimonad,