module Lens.Family.State.Lazy
( focus
, access
, (%=)
, (~=)
, (%%=)
, (+=), (-=), (*=)
, (//=)
, (&&=), (||=)
, (<>=)
, Focusing
) where
import Data.Monoid (Monoid, mappend)
import Control.Monad (liftM)
import Control.Monad.Trans.State.Lazy (StateT(..), get, modify)
import Lens.Family (Getter, Setter, (^.), (%~))
import Lens.Family.Stock (Ref)
import Lens.Family.State.Focus (Focusing(..))
focus :: Monad m => Ref (Focusing m c) a b -> StateT b m c -> StateT a m c
focus l m = StateT $ unFocusing . l (Focusing . (runStateT m))
access :: Monad m => Getter a b -> StateT a m b
access l = (^. l) `liftM` get
infix 4 %=
(%=) :: Monad m => Setter a b -> (b -> b) -> StateT a m ()
l %= f = modify (l %~ f)
infix 4 ~=
(~=) :: Monad m => Setter a b -> b -> StateT a m ()
l ~= v = l %= const v
infix 4 %%=
(%%=) :: Monad m => Ref (Focusing m c) a b -> (b -> (c, b)) -> StateT a m c
l %%= f = focus l (StateT (return . f))
infixr 4 +=, -=, *=
(+=), (-=), (*=) :: (Monad m, Num b) => Setter a b -> b -> StateT a m ()
f += b = f %= (+ b)
f -= b = f %= subtract b
f *= b = f %= (* b)
infixr 4 //=
(//=) :: (Monad m, Fractional b) => Setter a b -> b -> StateT a m ()
f //= b = f %= (/ b)
infixr 4 &&=, ||=
(&&=), (||=) :: Monad m => Setter a Bool -> Bool -> StateT a m ()
f &&= b = f %= (&& b)
f ||= b = f %= (|| b)
infixr 4 <>=
(<>=) :: (Monoid o, Monad m) => Setter a o -> o -> StateT a m ()
f <>= b = f %= (`mappend` b)