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.
§Type Parameters
A: The “loop” type - when we returnLoop(a), we continue witha.B: The “done” type - when we returnDone(b), we’re finished.
§Variants
Loop(A): Continue the loop with a new value.Done(B): Finish the computation with a final value.
§Examples
use fp_library::types::*;
let loop_step: Step<i32, i32> = Step::Loop(10);
let done_step: Step<i32, i32> = Step::Done(20);Variants§
Implementations§
Source§impl<A, B> Step<A, B>
impl<A, B> Step<A, B>
Sourcepub fn map_loop<C>(self, f: impl FnOnce(A) -> C) -> Step<C, B>
pub fn map_loop<C>(self, f: impl FnOnce(A) -> C) -> Step<C, B>
Maps a function over the Loop variant.
§Type Signature
forall c. (A -> c) -> Step c B
§Type Parameters
C: The new loop type.
§Parameters
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));Sourcepub fn map_done<C>(self, f: impl FnOnce(B) -> C) -> Step<A, C>
pub fn map_done<C>(self, f: impl FnOnce(B) -> C) -> Step<A, C>
Maps a function over the Done variant.
§Type Signature
forall c. (B -> c) -> Step A c
§Type Parameters
C: The new done type.
§Parameters
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));Sourcepub fn bimap<C, D>(
self,
f: impl FnOnce(A) -> C,
g: impl FnOnce(B) -> D,
) -> Step<C, D>
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 c d. (A -> c, B -> d) -> Step c d
§Type Parameters
C: The new loop type.D: The new done type.
§Parameters
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));Trait Implementations§
impl<A: Copy, B: Copy> Copy for Step<A, B>
impl<A: Eq, B: Eq> Eq for Step<A, B>
impl<A, B> StructuralPartialEq for Step<A, B>
Auto Trait Implementations§
impl<A, B> Freeze for Step<A, B>
impl<A, B> RefUnwindSafe for Step<A, B>where
A: RefUnwindSafe,
B: RefUnwindSafe,
impl<A, B> Send for Step<A, B>
impl<A, B> Sync for Step<A, B>
impl<A, B> Unpin for Step<A, B>
impl<A, B> UnwindSafe for Step<A, B>where
A: UnwindSafe,
B: 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
Mutably borrows from an owned value. Read more
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>
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 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>
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