relude-1.2.2.0: Safe, performant, user-friendly and lightweight Haskell Standard Library

Copyright | (c) 2016 Stephen Diehl (c) 2016-2018 Serokell (c) 2018-2023 Kowainik
License | MIT
Maintainer | Kowainik <xrom.xkov@gmail.com>
Stability | Stable
Portability | Portable
Safe Haskell | Trustworthy
Language | Haskell2010

Relude.Monad.Trans

Contents

- Convenient functions to work with `Reader` monad
- Convenient functions to work with `State` monad
- Lifted to Transformers

Description

Monad transformers utilities.

Synopsis

- usingReader :: r -> Reader r a -> a
- usingReaderT :: r -> ReaderT r m a -> m a
- etaReaderT :: forall r (m :: Type -> Type) a. ReaderT r m a -> ReaderT r m a
- evaluatingState :: s -> State s a -> a
- evaluatingStateT :: Functor f => s -> StateT s f a -> f a
- executingState :: s -> State s a -> s
- executingStateT :: Functor f => s -> StateT s f a -> f s
- usingState :: s -> State s a -> (a, s)
- usingStateT :: s -> StateT s m a -> m (a, s)
- hoistMaybe :: forall (m :: Type -> Type) a. Applicative m => Maybe a -> MaybeT m a
- hoistEither :: forall (m :: Type -> Type) e a. Applicative m => Either e a -> ExceptT e m a

# Convenient functions to work with `Reader` monad

usingReader :: r -> Reader r a -> a #

Shorter and more readable alias for `flip runReader`.

    >>> usingReader 42 $ asks (+5)
    47

usingReaderT :: r -> ReaderT r m a -> m a #

Shorter and more readable alias for `flip runReaderT`.

    >>> usingReaderT 42 $ asks (+5)
    47

etaReaderT :: forall r (m :: Type -> Type) a. ReaderT r m a -> ReaderT r m a #

This function helps with optimizing performance when working with the `ReaderT` transformer. If you have code like below, that is called in a loop

    step :: Instruction -> ReaderT Config IO Result
    step instruction = case instruction of
        Add -> do stuff ...
        Del -> do stuff ...

you can improve performance of your Haskell applications by using `etaReaderT` in the following way:

    step :: Instruction -> ReaderT Config IO Result
    step instruction = etaReaderT $ case instruction of
        Add -> do stuff ...
        Del -> do stuff ...

For a detailed explanation, refer to the following blog post:

- Faster Winter 5: Eta-Expanding ReaderT (by Joachim Breitner)

Since: 0.7.0.0

# Convenient functions to work with `State` monad

evaluatingState :: s -> State s a -> a #

Alias for `flip evalState`. It's not shorter but sometimes more readable. Done by analogy with `using*` functions family.

evaluatingStateT :: Functor f => s -> StateT s f a -> f a #

Alias for `flip evalStateT`. It's not shorter but sometimes more readable. Done by analogy with `using*` functions family.

executingState :: s -> State s a -> s #

Alias for `flip execState`. It's not shorter but sometimes more readable. Done by analogy with `using*` functions family.

executingStateT :: Functor f => s -> StateT s f a -> f s #

Alias for `flip execStateT`. It's not shorter but sometimes more readable. Done by analogy with `using*` functions family.

usingState :: s -> State s a -> (a, s) #

Shorter and more readable alias for `flip runState`.

usingStateT :: s -> StateT s m a -> m (a, s) #

Shorter and more readable alias for `flip runStateT`.

    >>> usingStateT 0 $ put 42 >> pure False
    (False,42)

# Lifted to Transformers

hoistMaybe :: forall (m :: Type -> Type) a. Applicative m => Maybe a -> MaybeT m a #

Lift a `Maybe` to the `MaybeT` monad

Since: 0.3.0

hoistEither :: forall (m :: Type -> Type) e a. Applicative m => Either e a -> ExceptT e m a #

Lift a `Either` to the `ExceptT` monad

Since: 0.3.0