Copyright | (c) 2015-2016, Peter Trško |
---|---|
License | BSD3 |
Maintainer | peter.trsko@gmail.com |
Stability | experimental |
Portability | CPP, DeriveFoldable, DeriveFunctor, DeriveDataTypeable, DeriveGeneric, DeriveTraversable, FlexibleInstances, NoImplicitPrelude, TypeFamilies |
Safe Haskell | None |
Language | Haskell2010 |
ApplyEndo
provides easier endomorphism evaluation in cases when there is an
"obvious" default value.
- newtype ApplyEndo t f a = ApplyEndo {
- applyEndo :: f a
- apply :: Applicative f => a -> Endo a -> ApplyEndo t f a
- applyF :: Functor f => a -> f (Endo a) -> ApplyEndo t f a
- data Mempty
- applyMempty :: Monoid a => ApplyEndo Mempty f a -> f a
- applyMempty_ :: Monoid a => ApplyEndo Mempty Identity a -> a
- joinApplyMempty :: (Monad m, Monoid a) => m (ApplyEndo Mempty m a) -> m a
- data Def
- applyDef :: (Applicative f, Default a) => ApplyEndo Def f a -> f a
- applyDef_ :: Default a => ApplyEndo Def Identity a -> a
- joinApplyDef :: (Monad m, Default a) => m (ApplyEndo Def m a) -> m a
- data Reader
- applyReader :: MonadReader r m => ApplyEndo Reader m r -> m r
- applyReaderWith :: MonadReader r m => (m r -> a) -> ApplyEndo Reader m r -> a
- joinApplyReader :: MonadReader r m => m (ApplyEndo Reader m r) -> m r
- data Modify
- applyModify :: MonadState s m => ApplyEndo Modify m s -> m s
- joinApplyModify :: MonadState s m => m (ApplyEndo Modify m s) -> m s
- data Modify'
- applyModify' :: MonadState r m => ApplyEndo Modify' m () -> m ()
- joinApplyModify' :: MonadState r m => m (ApplyEndo Modify' m r) -> m r
ApplyEndo
newtype ApplyEndo t f a Source
There are cases when it is "obvious" what is the default value, which
should be modified by the endomorphism. This type is a result of such
endomorphism application and it uses phantom type t
as distinguishing
property, which decides what is the correct "default value".
apply :: Applicative f => a -> Endo a -> ApplyEndo t f a Source
Apply endomorphism using provided "default" value.
ApplyEndo Mempty
applyMempty :: Monoid a => ApplyEndo Mempty f a -> f a Source
Constrained version of applyEndo
. Usage example:
applyMempty . fromEndo :: (Applicative
f,Monoid
a) =>Endo
a -> f a
applyMempty_ :: Monoid a => ApplyEndo Mempty Identity a -> a Source
Same as applyMempty
, but Applicative
functor is specialized to
Identity
functor and evaluated.
Examples:
>>>
fromEndoWith applyMempty_ $ foldEndo (+1) [(*10), (+42)] :: Int
421>>>
fromEndoWith applyMempty_ $ dualFoldEndo (+1) [(*10), (+42)] :: Int
52
joinApplyMempty :: (Monad m, Monoid a) => m (ApplyEndo Mempty m a) -> m a Source
Evaluates ApplyEndo
in a Monad
by joining it with the monad it
contains. It can be also viewed as a variant of applyMempty
defined as:
joinApplyMempty
= (>>=
applyMempty
)
ApplyEndo Def
Apply endomorphism to a default value def
from Default
. See also
following packages:
Both of those packages provide additional instances to Default
type
class.
applyDef :: (Applicative f, Default a) => ApplyEndo Def f a -> f a Source
Constrained version of applyEndo
. Usage example:
applyDef . fromEndo :: (Applicative
f,Default
a) =>Endo
a -> f a
applyDef_ :: Default a => ApplyEndo Def Identity a -> a Source
Same as applyDef
, but Applicative
functor is specialized to Identity
functor and evaluated.
Examples:
>>>
fromEndoWith applyDef_ $ foldEndo (+1) [(*10), (+42)] :: Int
421>>>
fromEndoWith applyDef_ $ dualFoldEndo (+1) [(*10), (+42)] :: Int
52
ApplyEndo Reader
applyReader :: MonadReader r m => ApplyEndo Reader m r -> m r Source
Evaluates ApplyEndo
in terms of asks
operation.
This (->) r
is a valid MonadReader
instance, therefore, this is a valid
use case:
>>>
(applyReader . fromEndo $ foldEndo (*10) (+1)) 0 :: Int
10
applyReaderWith :: MonadReader r m => (m r -> a) -> ApplyEndo Reader m r -> a Source
Evaluates ApplyEndo
in terms of asks
operation and then
evaluates the resalt using provided function.
This (->) r
is a valid MonadReader
instance, therefore, this is a valid
use case:
>>>
applyReaderWith ($ 0) . fromEndo $ foldEndo (*10) (+1) :: Int
10
joinApplyReader :: MonadReader r m => m (ApplyEndo Reader m r) -> m r Source
Evaluates ApplyEndo
in a Monad
by joining it with the monad it
contains. It can be also viewed as a variant of applyReader
defined as:
joinApplyReader
= (>>=
applyReader
)
ApplyEndo Modify
applyModify :: MonadState s m => ApplyEndo Modify m s -> m s Source
joinApplyModify :: MonadState s m => m (ApplyEndo Modify m s) -> m s Source
Evaluates ApplyEndo
in a Monad
by joining it with the monad it
contains. It can be also viewed as a variant of applyModify
defined as:
joinApplyModify
= (>>=
applyModify
)
ApplyEndo Modify'
Same as Modify
, but strictness is implied.
applyModify' :: MonadState r m => ApplyEndo Modify' m () -> m () Source
joinApplyModify' :: MonadState r m => m (ApplyEndo Modify' m r) -> m r Source
Evaluates ApplyEndo
in a Monad
by joining it with the monad it
contains. It can be also viewed as a variant of applyModify'
defined as:
joinApplyModify'
= (>>=
applyModify'
)