module Lens.Family.State.Lazy
( zoom
, use, uses
, (%=)
, assign, (.=)
, (%%=)
, (<~)
, (+=), (-=), (*=)
, (//=)
, (&&=), (||=)
, (<>=)
, Zooming
, LensLike, LensLike'
, FoldLike, Constant
, ASetter, ASetter', Identity
, StateT, Writer
, Monoid
) where
import Data.Monoid (Monoid, mappend)
import Data.Tuple (swap)
import Control.Monad (liftM)
import Control.Monad.Trans.Writer.Lazy (Writer, writer, runWriter)
import Control.Monad.Trans.State.Lazy (StateT(..), state, get, modify)
import Lens.Family ( LensLike, LensLike'
, FoldLike, Constant
, ASetter, ASetter', Identity
, view, views, (%~)
)
import Lens.Family.State.Zoom (Zooming(..))
zoom :: Monad m => LensLike' (Zooming m c) a b -> StateT b m c -> StateT a m c
zoom l m = StateT $ unZooming . l (Zooming . (runStateT m))
use :: Monad m => FoldLike b a a' b b' -> StateT a m b
use l = view l `liftM` get
uses :: Monad m => FoldLike r a a' b b' -> (b -> r) -> StateT a m r
uses l f = views l f `liftM` get
infix 4 %=
(%=) :: Monad m => ASetter a a b b' -> (b -> b') -> StateT a m ()
l %= f = modify (l %~ f)
infix 4 .=
(.=) :: Monad m => ASetter a a b b' -> b' -> StateT a m ()
l .= v = l %= const v
assign :: Monad m => ASetter a a b b' -> b' -> StateT a m ()
assign = (.=)
infixr 2 <~
(<~) :: Monad m => ASetter a a b b' -> StateT a m b' -> StateT a m ()
l <~ v = assign l =<< v
infix 4 %%=
(%%=) :: Monad m => LensLike (Writer c) a a b b' -> (b -> (c, b')) -> StateT a m c
l %%= f = state (swap . runWriter . l (writer . swap . f))
infixr 4 +=, -=, *=
(+=), (-=), (*=) :: (Monad m, Num b) => ASetter' 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) => ASetter' a b -> b -> StateT a m ()
f //= b = f %= (/ b)
infixr 4 &&=, ||=
(&&=), (||=) :: Monad m => ASetter' a Bool -> Bool -> StateT a m ()
f &&= b = f %= (&& b)
f ||= b = f %= (|| b)
infixr 4 <>=
(<>=) :: (Monoid o, Monad m) => ASetter' a o -> o -> StateT a m ()
f <>= b = f %= (`mappend` b)