effectful-core-2.3.0.1: An easy to use, performant extensible effects library.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Effectful.Writer.Static.Local

Description

Support for access to a write only value of a particular type.

The value is thread local. If you want it to be shared between threads, use Effectful.Writer.Static.Shared.

Warning: Writer's state will be accumulated via left-associated uses of <>, which makes it unsuitable for use with types for which such pattern is inefficient. This applies, in particular, to the standard list type, which makes the Writer effect pretty niche.

Note: while the Writer from the transformers package includes additional operations pass and censor, they don't cooperate with runtime exceptions very well, so they're deliberately omitted here.

Synopsis

Effect

data Writer w :: Effect Source #

Provide access to a strict (WHNF), thread local, write only value of type w.

Instances

Instances details
type DispatchOf (Writer w) Source # 
Instance details

Defined in Effectful.Writer.Static.Local

newtype StaticRep (Writer w) Source # 
Instance details

Defined in Effectful.Writer.Static.Local

newtype StaticRep (Writer w) = Writer w

Handlers

runWriter :: Monoid w => Eff (Writer w ': es) a -> Eff es (a, w) Source #

Run a Writer effect and return the final value along with the final output.

execWriter :: Monoid w => Eff (Writer w ': es) a -> Eff es w Source #

Run a Writer effect and return the final output, discarding the final value.

Operations

tell :: (Writer w :> es, Monoid w) => w -> Eff es () Source #

Append the given output to the overall output of the Writer.

listen :: (Writer w :> es, Monoid w) => Eff es a -> Eff es (a, w) Source #

Execute an action and append its output to the overall output of the Writer.

Note: if an exception is received while the action is executed, the partial output of the action will still be appended to the overall output of the Writer:

>>> :{
  runEff . execWriter @String $ do
    tell "Hi"
    handle (\(_::ErrorCall) -> pure ((), "")) $ do
      tell " there"
      listen $ do
        tell "!"
        error "oops"
:}
"Hi there!"

listens :: (Writer w :> es, Monoid w) => (w -> b) -> Eff es a -> Eff es (a, b) Source #

Execute an action and append its output to the overall output of the Writer, then return the final value along with a function of the recorded output.

listens f m ≡ second f <$> listen m