IStateClass

This commit is contained in:
AF 2023-04-25 01:55:23 +00:00
parent 77846511b2
commit 2ef556a71e
2 changed files with 46 additions and 9 deletions

View File

@ -11,11 +11,15 @@ pub mod classes;
pub mod clone_func; pub mod clone_func;
pub mod copy_func; pub mod copy_func;
pub mod derivations; pub mod derivations;
mod istate;
#[cfg(test)] #[cfg(test)]
pub mod test_suite; pub mod test_suite;
#[cfg(test)] #[cfg(test)]
pub mod tests; pub mod tests;
pub use self::istate::IState;
use self::istate::IStateClass;
/// Part of Haskell's `Functor f` responsible to use `f a`. /// Part of Haskell's `Functor f` responsible to use `f a`.
/// ///
/// <https://hackage.haskell.org/package/base-4.18.0.0/docs/Data-Functor.html> /// <https://hackage.haskell.org/package/base-4.18.0.0/docs/Data-Functor.html>
@ -153,14 +157,6 @@ pub trait Applicative: Pure + ApplicativeSeq + ApplicativeLA2 + ApplicativeTuple
} }
} }
/// Represents iteration state.
pub enum IState<A, B> {
/// Loop running.
Pending(A),
/// Loop finished.
Done(B),
}
/// Equivalent of Haskell's `Monad`. /// Equivalent of Haskell's `Monad`.
/// ///
/// <https://hackage.haskell.org/package/base-4.18.0.0/docs/Control-Monad.html> /// <https://hackage.haskell.org/package/base-4.18.0.0/docs/Control-Monad.html>
@ -226,7 +222,10 @@ pub trait LocalFunctor: WeakFunctor {
/// Extract iteration state, if successful. /// Extract iteration state, if successful.
fn unstuff<'a, A: 'a, B: 'a>(state: Self::F<'a, IState<A, B>>) -> IState<A, Self::F<'a, B>> fn unstuff<'a, A: 'a, B: 'a>(state: Self::F<'a, IState<A, B>>) -> IState<A, Self::F<'a, B>>
where where
Self: 'a; Self: 'a,
{
Self::stuff::<_, IStateClass<A>>(state)
}
/// Stuff wrapped result into another functor. /// Stuff wrapped result into another functor.
fn stuff<'a, A: 'a, T: 'a + Pure>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>> fn stuff<'a, A: 'a, T: 'a + Pure>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>>

38
src/func/istate.rs Normal file
View File

@ -0,0 +1,38 @@
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)
}
}