module Graphics.Rasterific.MiniLens
(
Lens
, Lens'
, Traversal
, Traversal'
, (.^)
, use
, (.~)
, (.=)
, (%=)
, (+=)
) where
import Control.Monad.Identity
import Control.Applicative
import Control.Monad.State as State
infixl 8 .^
infixr 4 .~
infix 4 .=,%=,+=
type Lens s t a b =
forall f. Functor f => (a -> f b) -> s -> f t
type Lens' s a = Lens s s a a
type Traversal s t a b =
forall f. Applicative f => (a -> f b) -> s -> f t
type Traversal' s a = Traversal s s a a
(.^) :: s -> Lens s t a b -> a
(.^) v l = getConst (l Const v)
(.~) :: s -> Lens' s a -> a -> s
(.~) v l new = runIdentity $ l (\_ -> Identity new) v
(.=) :: MonadState s m => Lens' s a -> a -> m ()
(.=) l v = State.modify $ \s -> (s .~ l) v
(%=) :: MonadState s m => Lens' s a -> (a -> a) -> m ()
(%=) l f = State.modify $ \s -> (s .~ l) $ f (s .^ l)
(+=) :: (Num a, MonadState s m) => Lens' s a -> a -> m ()
(+=) l n = l %= (+ n)
use :: MonadState s m => Lens s t a b -> m a
use l = State.gets (.^ l)