module Lens.Family2.State.Lazy
( focus
, access
, (%=)
, (~=)
, (%%=)
, (+=), (-=), (*=)
, (//=)
, (&&=), (||=)
, (<>=)
) where
import Data.Monoid (Monoid, mappend)
import Control.Monad (liftM)
import Control.Monad.State.Lazy (MonadState, StateT(..), get, modify, state)
import Lens.Family (Getter, Setter, (^.), (%~))
import qualified Lens.Family.State.Lazy as LFS
import Lens.Family2.Stock (Lens)
focus :: Monad m => Lens a b -> StateT b m c -> StateT a m c
focus l = LFS.focus l
access :: MonadState a m => Getter a b -> m b
access l = (^. l) `liftM` get
infix 4 %=
(%=) :: MonadState a m => Setter a b -> (b -> b) -> m ()
l %= f = modify (l %~ f)
infix 4 ~=
(~=) :: MonadState a m => Setter a b -> b -> m ()
l ~= v = l %= const v
infix 4 %%=
(%%=) :: MonadState a m => Lens a b -> (b -> (c, b)) -> m c
l %%= f = state (l f)
infixr 4 +=, -=, *=
(+=), (-=), (*=) :: (MonadState a m, Num b) => Setter a b -> b -> m ()
f += b = f %= (+ b)
f -= b = f %= subtract b
f *= b = f %= (* b)
infixr 4 //=
(//=) :: (MonadState a m, Fractional b) => Setter a b -> b -> m ()
f //= b = f %= (/ b)
infixr 4 &&=, ||=
(&&=), (||=) :: MonadState a m => Setter a Bool -> Bool -> m ()
f &&= b = f %= (&& b)
f ||= b = f %= (|| b)
infixr 4 <>=
(<>=) :: (Monoid o, MonadState a m) => Setter a o -> o -> m ()
f <>= b = f %= (`mappend` b)