{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TypeFamilies #-}
module Serokell.Util.Lens
( (%%=)
, (%?=)
, zoom'
, magnify'
) where
import Universum
import Control.Monad.Identity (Identity)
import Control.Monad.Trans.Except (ExceptT, mapExceptT)
import Lens.Micro.Mtl ((.=))
import Lens.Micro as L
import Lens.Micro.Mtl as LM
import Lens.Micro.Mtl.Internal as LMI
infix 4 %%=
(%%=) :: L.Lens' s a -> State a b -> State s b
(%%=) l ma = do
attr <- LM.view l <$> get
let (res,newAttr) = runState ma attr
l .= newAttr
return res
infix 4 %?=
(%?=) :: L.Lens' s a -> ExceptT t (State a) b -> ExceptT t (State s) b
(%?=) l = mapExceptT (l %%=)
zoom'
:: MonadState s m
=> L.LensLike' (LMI.Zoomed (State s) a) s t
-> StateT t Identity a
-> m a
zoom' l = state . runState . LM.zoom l
magnify'
:: MonadReader s m
=> L.LensLike' (LMI.Magnified (Reader s) a) s t
-> ReaderT t Identity a
-> m a
magnify' l = reader . runReader . LM.magnify l