| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Traq.Control.Monad
Synopsis
- mapAccumM :: (Monad m, Traversable t) => (s -> a -> m (s, b)) -> s -> t a -> m (s, t b)
- forAccumM :: (Monad m, Traversable t) => s -> t a -> (s -> a -> m (s, b)) -> m (s, t b)
- withSandboxOf :: MonadState s m => Lens' s s' -> m a -> m a
- withSandbox :: MonadState s m => m a -> m a
- tell :: MonadWriter w m => w -> m ()
- tellAt :: MonadWriter w m => Lens' w w' -> w' -> m ()
- writeElemAt :: (Applicative f, MonadWriter w m) => Lens' w (f a) -> a -> m ()
- writeElem :: (Applicative f, MonadWriter (f a) m) => a -> m ()
- ignoreWriter :: (Monad m, Monoid w') => RWST r w s m a -> RWST r w' s m a
- throwFrom :: MonadError MyError m => m a -> MyError -> m a
- singularM :: MonadError e m => Getting (Endo [a]) s a -> e -> s -> m a
- maybeWithError :: MonadError e m => e -> Maybe a -> m a
- liftEither :: MonadError e m => Either e a -> m a
- (??) :: Functor f => f (a -> b) -> a -> f b
- non' :: a -> SimpleGetter (Maybe a) a
Traversable
mapAccumM :: (Monad m, Traversable t) => (s -> a -> m (s, b)) -> s -> t a -> m (s, t b) #
The mapAccumM function behaves like a combination of mapM and
mapAccumL that traverses the structure while evaluating the actions
and passing an accumulating parameter from left to right.
It returns a final value of this accumulator together with the new structure.
The accummulator is often used for caching the intermediate results of a computation.
Examples
Basic usage:
>>>let expensiveDouble a = putStrLn ("Doubling " <> show a) >> pure (2 * a)>>>:{mapAccumM (\cache a -> case lookup a cache of Nothing -> expensiveDouble a >>= \double -> pure ((a, double):cache, double) Just double -> pure (cache, double) ) [] [1, 2, 3, 1, 2, 3] :} Doubling 1 Doubling 2 Doubling 3 ([(3,6),(2,4),(1,2)],[2,4,6,2,4,6])
Since: base-4.18.0.0
forAccumM :: (Monad m, Traversable t) => s -> t a -> (s -> a -> m (s, b)) -> m (s, t b) #
MonadState
withSandboxOf :: MonadState s m => Lens' s s' -> m a -> m a #
Save the current state, run a computation and restore the saved state.
withSandbox :: MonadState s m => m a -> m a #
Save the current state, run a computation and restore the saved state.
MonadWriter
tell :: MonadWriter w m => w -> m () #
is an action that produces the output tell ww.
tellAt :: MonadWriter w m => Lens' w w' -> w' -> m () #
Write at a particular location in the monoid.
writeElemAt :: (Applicative f, MonadWriter w m) => Lens' w (f a) -> a -> m () #
writeElem :: (Applicative f, MonadWriter (f a) m) => a -> m () #
MonadError
throwFrom :: MonadError MyError m => m a -> MyError -> m a #
try-catch block that prepends a message to the existing error, to produce a more verbose backtrace.
singularM :: MonadError e m => Getting (Endo [a]) s a -> e -> s -> m a #
extract a singular value from a traversal, throwing an error if there are 0/more than 1.
maybeWithError :: MonadError e m => e -> Maybe a -> m a #
lift a Maybe to a value, throwing an error if Nothing
liftEither :: MonadError e m => Either e a -> m a #
lift a Maybe to a value, throwing an error if Nothing
Helpers
non' :: a -> SimpleGetter (Maybe a) a #
Getter to set Nothing to a default value.