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