From 2ef556a71eea389c6f88c197349a48e69227567f Mon Sep 17 00:00:00 2001 From: timofey Date: Tue, 25 Apr 2023 01:55:23 +0000 Subject: [PATCH] IStateClass --- src/func.rs | 17 ++++++++--------- src/func/istate.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 src/func/istate.rs diff --git a/src/func.rs b/src/func.rs index ac90710..41a38a0 100644 --- a/src/func.rs +++ b/src/func.rs @@ -11,11 +11,15 @@ pub mod classes; pub mod clone_func; pub mod copy_func; pub mod derivations; +mod istate; #[cfg(test)] pub mod test_suite; #[cfg(test)] pub mod tests; +pub use self::istate::IState; +use self::istate::IStateClass; + /// Part of Haskell's `Functor f` responsible to use `f a`. /// /// @@ -153,14 +157,6 @@ pub trait Applicative: Pure + ApplicativeSeq + ApplicativeLA2 + ApplicativeTuple } } -/// Represents iteration state. -pub enum IState { - /// Loop running. - Pending(A), - /// Loop finished. - Done(B), -} - /// Equivalent of Haskell's `Monad`. /// /// @@ -226,7 +222,10 @@ pub trait LocalFunctor: WeakFunctor { /// Extract iteration state, if successful. fn unstuff<'a, A: 'a, B: 'a>(state: Self::F<'a, IState>) -> IState> where - Self: 'a; + Self: 'a, + { + Self::stuff::<_, IStateClass>(state) + } /// 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>> diff --git a/src/func/istate.rs b/src/func/istate.rs new file mode 100644 index 0000000..913290c --- /dev/null +++ b/src/func/istate.rs @@ -0,0 +1,38 @@ +use super::*; + +/// Represents iteration state. +pub enum IState { + /// Loop running. + Pending(P), + /// Loop finished. + Done(D), +} + +pub struct IStateClass

(P); + +impl

WeakFunctor for IStateClass

{ + type F<'a, A: 'a> = IState + where + Self: 'a; +} + +impl

Functor for IStateClass

{ + 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

Pure for IStateClass

{ + fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> + where + Self: 'a, + { + IState::Done(a) + } +}