radn-rs/src/func/istate.rs
2023-04-25 01:55:23 +00:00

39 lines
746 B
Rust

use super::*;
/// Represents iteration state.
pub enum IState<P, D> {
/// Loop running.
Pending(P),
/// Loop finished.
Done(D),
}
pub struct IStateClass<P>(P);
impl<P> WeakFunctor for IStateClass<P> {
type F<'a, A: 'a> = IState<P, A>
where
Self: 'a;
}
impl<P> Functor for IStateClass<P> {
fn fmap<'a, A: 'a, B: 'a>(f: impl 'a + FnOnce(A) -> B, fa: Self::F<'a, A>) -> Self::F<'a, B>
where
Self: 'a,
{
match fa {
IState::Pending(p) => IState::Pending(p),
IState::Done(d) => IState::Done(f(d)),
}
}
}
impl<P> Pure for IStateClass<P> {
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A>
where
Self: 'a,
{
IState::Done(a)
}
}