Skip to main content

Step

Enum Step 

Source
pub enum Step<A, B> {
    Loop(A),
    Done(B),
}
Expand description

Represents the result of a single step in a tail-recursive computation.

This type is fundamental to stack-safe recursion via MonadRec.

§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

  • A: The “loop” type - when we return Loop(a), we continue with a.
  • B: The “done” type - when we return Done(b), we’re finished.

§Variants

  • Loop(A): Continue the loop with a new value.
  • Done(B): Finish the computation with a final value.

Variants§

§

Loop(A)

Continue the loop with a new value

§

Done(B)

Finish the computation with a final value

Implementations§

Source§

impl<A, B> Step<A, B>

§Type Parameters
  • 'a: The lifetime of the values.
  • A: The type of the Loop value.
  • B: The type of the Done value.
Source

pub fn is_loop(&self) -> bool

Returns true if this is a Loop variant.

§Type Signature

forall A B. &Step A B -> bool

§Returns

true if the step is a loop, false otherwise.

§Examples
use fp_library::types::*;

let step: Step<i32, i32> = Step::Loop(1);
assert!(step.is_loop());
Source

pub fn is_done(&self) -> bool

Returns true if this is a Done variant.

§Type Signature

forall A B. &Step A B -> bool

§Returns

true if the step is done, false otherwise.

§Examples
use fp_library::types::*;

let step: Step<i32, i32> = Step::Done(1);
assert!(step.is_done());
Source

pub fn map_loop<C>(self, f: impl FnOnce(A) -> C) -> Step<C, B>

Maps a function over the Loop variant.

§Type Signature

forall A B C. (Step A B, A -> C) -> Step C B

§Type Parameters
  • C: The new loop type.
§Parameters
  • self: The step value.
  • f: The function to apply to the loop value.
§Returns

A new Step with the loop value transformed.

§Examples
use fp_library::types::*;

let step: Step<i32, i32> = Step::Loop(1);
let mapped = step.map_loop(|x| x + 1);
assert_eq!(mapped, Step::Loop(2));
Source

pub fn map_done<C>(self, f: impl FnOnce(B) -> C) -> Step<A, C>

Maps a function over the Done variant.

§Type Signature

forall A B C. (Step A B, B -> C) -> Step A C

§Type Parameters
  • C: The new done type.
§Parameters
  • self: The step value.
  • f: The function to apply to the done value.
§Returns

A new Step with the done value transformed.

§Examples
use fp_library::types::*;

let step: Step<i32, i32> = Step::Done(1);
let mapped = step.map_done(|x| x + 1);
assert_eq!(mapped, Step::Done(2));
Source

pub fn bimap<C, D>( self, f: impl FnOnce(A) -> C, g: impl FnOnce(B) -> D, ) -> Step<C, D>

Applies functions to both variants (bifunctor map).

§Type Signature

forall A B C D. (Step A B, A -> C, B -> D) -> Step C D

§Type Parameters
  • C: The new loop type.
  • D: The new done type.
§Parameters
  • self: The step value.
  • f: The function to apply to the loop value.
  • g: The function to apply to the done value.
§Returns

A new Step with both values transformed.

§Examples
use fp_library::types::*;

let step: Step<i32, i32> = Step::Loop(1);
let mapped = step.bimap(|x| x + 1, |x| x * 2);
assert_eq!(mapped, Step::Loop(2));
Source

pub fn bi_fold_right<C>( self, f: impl FnOnce(A, C) -> C, g: impl FnOnce(B, C) -> C, z: C, ) -> C

Folds the step from right to left using two step functions.

See Bifoldable::bi_fold_right for the type class version.

§Type Signature

forall A B C. (Step A B, (A, C) -> C, (B, C) -> C, C) -> C

§Type Parameters
  • C: The accumulator type.
§Parameters
  • self: The step value.
  • f: The step function for the Loop variant.
  • g: The step function for the Done variant.
  • z: The initial accumulator.
§Returns

The result of folding.

§Examples
use fp_library::types::*;

let x: Step<i32, i32> = Step::Loop(3);
assert_eq!(x.bi_fold_right(|a, acc| acc - a, |b, acc| acc + b, 10), 7);
Source

pub fn bi_fold_left<C>( self, f: impl FnOnce(C, A) -> C, g: impl FnOnce(C, B) -> C, z: C, ) -> C

Folds the step from left to right using two step functions.

See Bifoldable::bi_fold_left for the type class version.

§Type Signature

forall A B C. (Step A B, (C, A) -> C, (C, B) -> C, C) -> C

§Type Parameters
  • C: The accumulator type.
§Parameters
  • self: The step value.
  • f: The step function for the Loop variant.
  • g: The step function for the Done variant.
  • z: The initial accumulator.
§Returns

The result of folding.

§Examples
use fp_library::types::*;

let x: Step<i32, i32> = Step::Done(5);
assert_eq!(x.bi_fold_left(|acc, a| acc - a, |acc, b| acc + b, 10), 15);
Source

pub fn bi_fold_map<M>(self, f: impl FnOnce(A) -> M, g: impl FnOnce(B) -> M) -> M

Maps the value to a monoid depending on the variant.

See Bifoldable::bi_fold_map for the type class version.

§Type Signature

forall A B M. (Step A B, A -> M, B -> M) -> M

§Type Parameters
  • M: The monoid type.
§Parameters
  • self: The step value.
  • f: The function mapping the Loop value to the monoid.
  • g: The function mapping the Done value to the monoid.
§Returns

The monoid value.

§Examples
use fp_library::types::*;

let x: Step<i32, i32> = Step::Loop(3);
assert_eq!(x.bi_fold_map(|a: i32| a.to_string(), |b: i32| b.to_string()), "3");
Source

pub fn fold_right<C>(self, f: impl FnOnce(B, C) -> C, initial: C) -> C

Folds the Done value, returning initial for Loop.

See Foldable::fold_right for the type class version (via StepLoopAppliedBrand).

§Type Signature

forall A B C. (Step A B, (B, C) -> C, C) -> C

§Type Parameters
  • C: The accumulator type.
§Parameters
  • self: The step value.
  • f: The function to apply to the Done value and the accumulator.
  • initial: The initial accumulator.
§Returns

The result of folding.

§Examples
use fp_library::types::*;

let x: Step<(), i32> = Step::Done(5);
assert_eq!(x.fold_right(|b, acc| b + acc, 10), 15);
Source

pub fn fold_left<C>(self, f: impl FnOnce(C, B) -> C, initial: C) -> C

Folds the Done value from the left, returning initial for Loop.

See Foldable::fold_left for the type class version (via StepLoopAppliedBrand).

§Type Signature

forall A B C. (Step A B, (C, B) -> C, C) -> C

§Type Parameters
  • C: The accumulator type.
§Parameters
  • self: The step value.
  • f: The function to apply to the accumulator and the Done value.
  • initial: The initial accumulator.
§Returns

The result of folding.

§Examples
use fp_library::types::*;

let x: Step<(), i32> = Step::Done(5);
assert_eq!(x.fold_left(|acc, b| acc + b, 10), 15);
Source

pub fn fold_map<M: Monoid>(self, f: impl FnOnce(B) -> M) -> M

Maps the Done value to a monoid, returning M::empty() for Loop.

See Foldable::fold_map for the type class version (via StepLoopAppliedBrand).

§Type Signature

forall A B M. Monoid M => (Step A B, B -> M) -> M

§Type Parameters
  • M: The monoid type.
§Parameters
  • self: The step value.
  • f: The mapping function.
§Returns

The monoid value.

§Examples
use fp_library::types::*;

let x: Step<(), i32> = Step::Done(5);
assert_eq!(x.fold_map(|b: i32| b.to_string()), "5".to_string());
Source

pub fn bind<C>(self, f: impl FnOnce(B) -> Step<A, C>) -> Step<A, C>

Chains the Done value into a new computation, passing through Loop.

See Semimonad::bind for the type class version (via StepLoopAppliedBrand).

§Type Signature

forall A B C. (Step A B, B -> Step A C) -> Step A C

§Type Parameters
  • C: The type of the resulting Done value.
§Parameters
  • self: The step value.
  • f: The function to apply to the Done value.
§Returns

The result of the computation.

§Examples
use fp_library::types::*;

let x: Step<i32, i32> = Step::Done(5);
let y = x.bind(|b| Step::Done(b * 2));
assert_eq!(y, Step::Done(10));
Source

pub fn bind_loop<C>(self, f: impl FnOnce(A) -> Step<C, B>) -> Step<C, B>

Chains the Loop value into a new computation, passing through Done.

See Semimonad::bind for the type class version (via StepDoneAppliedBrand).

§Type Signature

forall A B C. (Step A B, A -> Step C B) -> Step C B

§Type Parameters
  • C: The type of the resulting Loop value.
§Parameters
  • self: The step value.
  • f: The function to apply to the Loop value.
§Returns

The result of the computation.

§Examples
use fp_library::types::*;

let x: Step<i32, i32> = Step::Loop(5);
let y = x.bind_loop(|a| Step::Loop(a * 2));
assert_eq!(y, Step::Loop(10));
Source§

impl<'a, A: 'a, B: 'a> Step<A, B>

§Type Parameters
  • 'a: The lifetime of the values.
  • A: The type of the Loop value.
  • B: The type of the Done value.
Source

pub fn bi_traverse<C: 'a + Clone, D: 'a + Clone, F: Applicative>( self, f: impl Fn(A) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, C> + 'a, g: impl Fn(B) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, D> + 'a, ) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, Step<C, D>>
where Step<C, D>: Clone,

Traverses the step with two effectful functions.

See Bitraversable::bi_traverse for the type class version.

§Type Signature

forall A B C D F. Applicative F => (Step A B, A -> F C, B -> F D) -> F (Step C D)

§Type Parameters
  • C: The output type for the Loop value.
  • D: The output type for the Done value.
  • F: The applicative context.
§Parameters
  • self: The step instance.
  • f: The function for the Loop value.
  • g: The function for the Done value.
§Returns

The transformed step wrapped in the applicative context.

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

let x: Step<i32, i32> = Step::Loop(3);
let y = x.bi_traverse::<_, _, OptionBrand>(|a| Some(a + 1), |b| Some(b * 2));
assert_eq!(y, Some(Step::Loop(4)));

Trait Implementations§

Source§

impl<A: Clone, B: Clone> Clone for Step<A, B>

Source§

fn clone(&self) -> Step<A, B>

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<A: Debug, B: Debug> Debug for Step<A, B>

Source§

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

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

impl<'de, A, B> Deserialize<'de> for Step<A, B>
where A: Deserialize<'de>, B: 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<A: Hash, B: Hash> Hash for Step<A, B>

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<A: PartialEq, B: PartialEq> PartialEq for Step<A, B>

Source§

fn eq(&self, other: &Step<A, B>) -> 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<A, B> Serialize for Step<A, B>
where A: Serialize, B: 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<A: Copy, B: Copy> Copy for Step<A, B>

Source§

impl<A: Eq, B: Eq> Eq for Step<A, B>

Source§

impl<A, B> StructuralPartialEq for Step<A, B>

Auto Trait Implementations§

§

impl<A, B> Freeze for Step<A, B>
where A: Freeze, B: Freeze,

§

impl<A, B> RefUnwindSafe for Step<A, B>

§

impl<A, B> Send for Step<A, B>
where A: Send, B: Send,

§

impl<A, B> Sync for Step<A, B>
where A: Sync, B: Sync,

§

impl<A, B> Unpin for Step<A, B>
where A: Unpin, B: Unpin,

§

impl<A, B> UnsafeUnpin for Step<A, B>
where A: UnsafeUnpin, B: UnsafeUnpin,

§

impl<A, B> UnwindSafe for Step<A, B>
where A: UnwindSafe, B: 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>,