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:
StepBrand: fully polymorphic over both loop and done types (bifunctor).StepLoopAppliedBrand<LoopType>: the loop type is fixed, polymorphic over the done type (functor over done).StepDoneAppliedBrand<DoneType>: the done type is fixed, polymorphic over the loop type (functor over loop).
§Serialization
This type supports serialization and deserialization via serde when the serde feature is enabled.
§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.
Variants§
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.
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.
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 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));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 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));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 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));Sourcepub fn bi_fold_right<C>(
self,
f: impl FnOnce(A, C) -> C,
g: impl FnOnce(B, C) -> C,
z: C,
) -> C
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);Sourcepub fn bi_fold_left<C>(
self,
f: impl FnOnce(C, A) -> C,
g: impl FnOnce(C, B) -> C,
z: C,
) -> C
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);Sourcepub fn bi_fold_map<M>(self, f: impl FnOnce(A) -> M, g: impl FnOnce(B) -> M) -> M
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");Sourcepub fn fold_right<C>(self, f: impl FnOnce(B, C) -> C, initial: C) -> C
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);Sourcepub fn fold_left<C>(self, f: impl FnOnce(C, B) -> C, initial: C) -> C
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);Sourcepub fn fold_map<M: Monoid>(self, f: impl FnOnce(B) -> M) -> M
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());Sourcepub fn bind<C>(self, f: impl FnOnce(B) -> Step<A, C>) -> Step<A, C>
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));Sourcepub fn bind_loop<C>(self, f: impl FnOnce(A) -> Step<C, B>) -> Step<C, B>
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.
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.
Sourcepub 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>>
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>>
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<'de, A, B> Deserialize<'de> for Step<A, B>where
A: Deserialize<'de>,
B: Deserialize<'de>,
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>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
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> 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> 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