| Safe Haskell | Safe-Inferred |
|---|---|
| Language | GHC2021 |
Effectful.State.Static.Shared
Contents
Description
Support for access to a shared, mutable value of a particular type.
The value is shared between multiple threads. If you want each thread to manage its own version of the value, use Effectful.State.Static.Local.
Note: unlike the StateT monad transformer from
the transformers library, the State effect doesn't discard state updates
when an exception is received:
>>>import Control.Exception (ErrorCall)>>>import Control.Monad.Catch>>>import Control.Monad.Trans.State.Strict qualified as S
>>>:{(`S.execStateT` "Hi") . handle (\(_::ErrorCall) -> pure ()) $ do S.modify (++ " there!") error "oops" :} "Hi"
>>>:{runEff . execState "Hi" . handle (\(_::ErrorCall) -> pure ()) $ do modify (++ " there!") error "oops" :} "Hi there!"
Synopsis
- data State (s :: Type) :: Effect
- runState :: HasCallStack => s -> Eff (State s : es) a -> Eff es (a, s)
- evalState :: HasCallStack => s -> Eff (State s : es) a -> Eff es a
- execState :: HasCallStack => s -> Eff (State s : es) a -> Eff es s
- runStateMVar :: HasCallStack => MVar s -> Eff (State s : es) a -> Eff es (a, s)
- evalStateMVar :: HasCallStack => MVar s -> Eff (State s : es) a -> Eff es a
- execStateMVar :: HasCallStack => MVar s -> Eff (State s : es) a -> Eff es s
- get :: (HasCallStack, State s :> es) => Eff es s
- gets :: (HasCallStack, State s :> es) => (s -> a) -> Eff es a
- put :: (HasCallStack, State s :> es) => s -> Eff es ()
- state :: (HasCallStack, State s :> es) => (s -> (a, s)) -> Eff es a
- modify :: (HasCallStack, State s :> es) => (s -> s) -> Eff es ()
- stateM :: (HasCallStack, State s :> es) => (s -> Eff es (a, s)) -> Eff es a
- modifyM :: (HasCallStack, State s :> es) => (s -> Eff es s) -> Eff es ()
Effect
data State (s :: Type) :: Effect Source #
Provide access to a strict (WHNF), shared, mutable value of type s.
Instances
| type DispatchOf (State s) Source # | |
Defined in Effectful.State.Static.Shared | |
| newtype StaticRep (State s) Source # | |
Defined in Effectful.State.Static.Shared | |
Handlers
runState :: HasCallStack => s -> Eff (State s : es) a -> Eff es (a, s) Source #
Run the State effect with the given initial state and return the final
value along with the final state.
evalState :: HasCallStack => s -> Eff (State s : es) a -> Eff es a Source #
Run the State effect with the given initial state and return the final
value, discarding the final state.
execState :: HasCallStack => s -> Eff (State s : es) a -> Eff es s Source #
Run the State effect with the given initial state and return the final
state, discarding the final value.
runStateMVar :: HasCallStack => MVar s -> Eff (State s : es) a -> Eff es (a, s) Source #
Deprecated: If you need access to the state from outside of the State effect, manage an explicit MVar yourself.
Run the State effect with the given initial state MVar and return the
final value along with the final state.
evalStateMVar :: HasCallStack => MVar s -> Eff (State s : es) a -> Eff es a Source #
Deprecated: If you need access to the state from outside of the State effect, manage an explicit MVar yourself.
Run the State effect with the given initial state MVar and return the
final value, discarding the final state.
execStateMVar :: HasCallStack => MVar s -> Eff (State s : es) a -> Eff es s Source #
Deprecated: If you need access to the state from outside of the State effect, manage an explicit MVar yourself.
Run the State effect with the given initial state MVar and return the
final state, discarding the final value.
Operations
put :: (HasCallStack, State s :> es) => s -> Eff es () Source #
Set the current state to the given value.
state :: (HasCallStack, State s :> es) => (s -> (a, s)) -> Eff es a Source #
Apply the function to the current state and return a value.
Note: this function gets an exclusive access to the state for its duration.
stateM :: (HasCallStack, State s :> es) => (s -> Eff es (a, s)) -> Eff es a Source #
Deprecated: Operations of the same State effect used within the callback deadlock. Use a combination of get and put instead, or an explicit MVar if you need atomic updates.
Apply the monadic function to the current state and return a value.
Note: this function gets an exclusive access to the state for its duration.
modifyM :: (HasCallStack, State s :> es) => (s -> Eff es s) -> Eff es () Source #
Deprecated: Operations of the same State effect used within the callback deadlock. Use a combination of get and put instead, or an explicit MVar if you need atomic updates.
Apply the monadic function to the current state.
modifyMf ≡stateM(\s -> ((), )<$>f s)
Note: this function gets an exclusive access to the state for its duration.