{-# LANGUAGE Rank2Types #-}
module Lens.Family2.State.Lazy
( LFS.zoom
, use, uses
, (%=)
, assign, (.=)
, (%%=)
, (<~)
, (+=), (-=), (*=)
, (//=)
, (&&=), (||=)
, (<>=)
, LFS.Zooming
, LensLike, LensLike'
, FoldLike, Constant
, Setter, Setter', Identical
, LFS.StateT, MonadState, 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.State.Lazy (MonadState, get, modify, state)
import Lens.Family2 ( LensLike, LensLike'
, FoldLike, Constant
, Setter, Setter', Identical
, view, views, (%~)
)
import qualified Lens.Family.State.Lazy as LFS
use :: MonadState a m => FoldLike b a a' b b' -> m b
use l = view l `liftM` get
uses :: MonadState a m => FoldLike r a a' b b' -> (b -> r) -> m r
uses l f = views l f `liftM` get
infix 4 %=
(%=) :: MonadState a m => Setter a a b b' -> (b -> b') -> m ()
l %= f = modify (l %~ f)
infix 4 .=
(.=) :: MonadState a m => Setter a a b b' -> b' -> m ()
l .= v = l %= const v
assign :: MonadState a m => Setter a a b b' -> b' -> m ()
assign = (.=)
infixr 2 <~
(<~) :: MonadState a m => Setter a a b b' -> m b' -> m ()
l <~ v = assign l =<< v
infix 4 %%=
(%%=) :: MonadState a m => LensLike (Writer c) a a b b' -> (b -> (c, b')) -> m c
l %%= f = state (swap . runWriter . l (writer . swap . 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)