packages feed

microlens-mtl 0.1.8.0 → 0.1.9.0

raw patch · 4 files changed

+80/−60 lines, 4 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

- Lens.Micro.Mtl: infix 4 <<.=
+ Lens.Micro.Mtl: (<~) :: MonadState s m => ASetter s s a b -> m b -> m ()
+ Lens.Micro.Mtl: (?=) :: MonadState s m => ASetter s s a (Maybe b) -> b -> m ()

Files

CHANGELOG.md view
@@ -1,3 +1,7 @@+# 0.1.9.0++* Added `?=` and `<~`.+ # 0.1.8.0  * Added `assign` and `modifying` as synonyms for `.=` and `%=`.
microlens-mtl.cabal view
@@ -1,5 +1,5 @@ name:                microlens-mtl-version:             0.1.8.0+version:             0.1.9.0 synopsis:            microlens support for Reader/Writer/State from mtl description:   This package contains functions (like 'view' or '+=') which work on 'MonadReader', 'MonadWriter', and 'MonadState' from the mtl package.
src/Lens/Micro/Mtl.hs view
@@ -23,18 +23,23 @@   view, preview,   use, preuse, -  -- * Zooming-  zoom,-  magnify,-   -- * Setting-  (.=), assign,   (%=), modifying,+  (.=), assign,+  (?=),+  (<~),++  -- * Specialised modifying operators+  -- $arith-note   (+=), (-=), (*=), (//=),    -- * Setting with passthrough   (<%=), (<<%=),   (<<.=),++  -- * Zooming+  zoom,+  magnify, ) where @@ -82,7 +87,7 @@ {-# INLINE preview #-}  {- |-'use' is ('^.) (or 'view') which implicitly operates on the state; for instance, if your state is a record containing a field @foo@, you can write+'use' is ('^.') (or 'view') which implicitly operates on the state; for instance, if your state is a record containing a field @foo@, you can write  @ x \<- 'use' foo@@ -114,9 +119,10 @@ {-# INLINE preuse #-}  -infix  4 .=, %=+infix  4 .=, %=, ?= infix  4 <<.=, <<%=, <%= infix  4 +=, -=, *=, //=+infixr 2 <~  {- | Modify state by “assigning” a value to a part of the state.@@ -141,6 +147,32 @@ {-# INLINE assign #-}  {- |+('?=') is a version of ('.=') that wraps the value into 'Just' before setting.++@+l '?=' b = l '.=' Just b+@++It can be useful in combination with 'at'.+-}+(?=) :: MonadState s m => ASetter s s a (Maybe b) -> b -> m ()+l ?= b = l .= Just b+{-# INLINE (?=) #-}++{- |+('<~') is a version of ('.=') that takes a monadic value (and then executes it and assigns the result to the lens).++@+l '<~' mb = do+  b <- mb+  l '.=' b+@+-}+(<~) :: MonadState s m => ASetter s s a b -> m b -> m ()+l <~ mb = mb >>= (l .=)+{-# INLINE (<~) #-}++{- | Modify state by applying a function to a part of the state. An example:  >>> execState (do _1 %= (+1); _2 %= reverse) (1,"hello")@@ -159,7 +191,7 @@ * ('+=') for addition * ('-=') for substraction * ('*=') for multiplication-* ('//=') for division (since ('/=') is already taken)+* ('//=') for division -} (%=) :: (MonadState s m) => ASetter s s a b -> (a -> b) -> m () l %= f = State.modify (l %~ f)@@ -172,46 +204,31 @@ modifying l f = l %= f {-# INLINE modifying #-} -{- |-Add a number to the target.+{- $arith-note +The following operators mimic well-known C operators ('+=', '-=', etc). ('//=') stands for division.++They're implemented like this:+ @ l '+=' x = l '%=' (+x)+l '-=' x = l '%=' ('subtract' x)+... @ -}+ (+=) :: (MonadState s m, Num a) => ASetter s s a a -> a -> m () l += x = l %= (+x) {-# INLINE (+=) #-} -{- |-Subtract a number from the target.--@-l '-=' x = l '%=' ('subtract' x)-@--} (-=) :: (MonadState s m, Num a) => ASetter s s a a -> a -> m () l -= x = l %= (subtract x) {-# INLINE (-=) #-} -{- |-Multiply the target by a number.--@-l '*=' x = l '%=' (*x)-@--} (*=) :: (MonadState s m, Num a) => ASetter s s a a -> a -> m () l *= x = l %= (*x) {-# INLINE (*=) #-} -{- |-Divide the target by a number.--@-l '//=' x = l '%=' (/x)-@--} (//=) :: (MonadState s m, Fractional a) => ASetter s s a a -> a -> m () l //= x = l %= (/x) {-# INLINE (//=) #-}
src/Lens/Micro/Mtl/Internal.hs view
@@ -62,7 +62,7 @@ -- Zoomed ------------------------------------------------------------------------------ --- | This type family is used by 'Control.Lens.Zoom.Zoom' to describe the common effect type.+-- | This type family is used by 'Zoom' to describe the common effect type. type family Zoomed (m :: * -> *) :: * -> * -> * type instance Zoomed (Strict.StateT s z) = Focusing z type instance Zoomed (Lazy.StateT s z) = Focusing z@@ -81,7 +81,7 @@ -- Focusing ------------------------------------------------------------------------------ --- | Used by 'Control.Lens.Zoom.Zoom' to 'Control.Lens.Zoom.zoom' into 'Control.Monad.State.StateT'.+-- | Used by 'Zoom' to 'zoom' into 'Control.Monad.State.StateT'. newtype Focusing m s a = Focusing { unfocusing :: m (s, a) }  instance Monad m => Functor (Focusing m s) where@@ -103,7 +103,7 @@ -- FocusingWith ------------------------------------------------------------------------------ --- | Used by 'Control.Lens.Zoom.Zoom' to 'Control.Lens.Zoom.zoom' into 'Control.Monad.RWS.RWST'.+-- | Used by 'Zoom' to 'zoom' into 'Control.Monad.RWS.RWST'. newtype FocusingWith w m s a = FocusingWith { unfocusingWith :: m (s, a, w) }  instance Monad m => Functor (FocusingWith w m s) where@@ -125,7 +125,7 @@ -- FocusingPlus ------------------------------------------------------------------------------ --- | Used by 'Control.Lens.Zoom.Zoom' to 'Control.Lens.Zoom.zoom' into 'Control.Monad.Writer.WriterT'.+-- | Used by 'Zoom' to 'zoom' into 'Control.Monad.Writer.WriterT'. newtype FocusingPlus w k s a = FocusingPlus { unfocusingPlus :: k (s, w) a }  instance Functor (k (s, w)) => Functor (FocusingPlus w k s) where@@ -142,7 +142,7 @@ -- FocusingOn ------------------------------------------------------------------------------ --- | Used by 'Control.Lens.Zoom.Zoom' to 'Control.Lens.Zoom.zoom' into 'Control.Monad.Trans.Maybe.MaybeT' or 'Control.Monad.Trans.List.ListT'.+-- | Used by 'Zoom' to 'zoom' into 'Control.Monad.Trans.Maybe.MaybeT' or 'Control.Monad.Trans.List.ListT'. newtype FocusingOn f k s a = FocusingOn { unfocusingOn :: k (f s) a }  instance Functor (k (f s)) => Functor (FocusingOn f k s) where@@ -174,7 +174,7 @@ -- FocusingMay ------------------------------------------------------------------------------ --- | Used by 'Control.Lens.Zoom.Zoom' to 'Control.Lens.Zoom.zoom' into 'Control.Monad.Error.ErrorT'.+-- | Used by 'Zoom' to 'zoom' into 'Control.Monad.Error.ErrorT'. newtype FocusingMay k s a = FocusingMay { unfocusingMay :: k (May s) a }  instance Functor (k (May s)) => Functor (FocusingMay k s) where@@ -206,7 +206,7 @@ -- FocusingErr ------------------------------------------------------------------------------ --- | Used by 'Control.Lens.Zoom.Zoom' to 'Control.Lens.Zoom.zoom' into 'Control.Monad.Error.ErrorT'.+-- | Used by 'Zoom' to 'zoom' into 'Control.Monad.Error.ErrorT'. newtype FocusingErr e k s a = FocusingErr { unfocusingErr :: k (Err e s) a }  instance Functor (k (Err e s)) => Functor (FocusingErr e k s) where@@ -254,8 +254,8 @@ @ moveNE :: 'Lazy.State' Game () moveNE = do-  player.position.x '+=' 1-  player.position.y '+=' 1+  player.position.x 'Lens.Micro.Mtl.+=' 1+  player.position.y 'Lens.Micro.Mtl.+=' 1 @  With 'zoom', you can use @player.position@ to focus just on a part of the state:@@ -264,23 +264,23 @@ moveNE :: 'Lazy.State' Game () moveNE = do   'zoom' (player.position) $ do-    x '+=' 1-    y '+=' 1+    x 'Lens.Micro.Mtl.+=' 1+    y 'Lens.Micro.Mtl.+=' 1 @  You can just as well use it for retrieving things out of the state:  @ getCoords :: 'Lazy.State' Game (Int, Int)-getCoords = 'zoom' (player.position) ((,) '<$>' 'use' x '<*>' 'use' y)+getCoords = 'zoom' (player.position) ((,) '<$>' 'Lens.Micro.Mtl.use' x '<*>' 'Lens.Micro.Mtl.use' y) @  Or more explicitly:  @ getCoords = 'zoom' (player.position) $ do-  x' <- 'use' x-  y' <- 'use' y+  x' <- 'Lens.Micro.Mtl.use' x+  y' <- 'Lens.Micro.Mtl.use' y   return (x', y') @ @@ -289,22 +289,22 @@ @ moveObstaclesNE :: 'Lazy.State' Game () moveObstaclesNE = do-  'zoom' (obstacles.each) $ do-    x '+=' 1-    y '+=' 1+  'zoom' (obstacles.'each') $ do+    x 'Lens.Micro.Mtl.+=' 1+    y 'Lens.Micro.Mtl.+=' 1 @  If the action returns a result, all results would be combined with '<>' – the same way they're combined when '^.' is passed a traversal. In this example, @moveObstaclesNE@ returns a list of old coordinates of obstacles in addition to moving them:  @ moveObstaclesNE = do-  xys <- 'zoom' (obstacles.each) $ do+  xys <- 'zoom' (obstacles.'each') $ do     -- Get old coordinates.-    x' <- 'use' x-    y' <- 'use' y+    x' <- 'Lens.Micro.Mtl.use' x+    y' <- 'Lens.Micro.Mtl.use' y     -- Update them.-    x '.=' x' + 1-    y '.=' y' + 1+    x 'Lens.Micro.Mtl..=' x' + 1+    y 'Lens.Micro.Mtl..=' y' + 1     -- Return a single-element list with old coordinates.     return [(x', y')]   ...@@ -366,7 +366,7 @@ -- Magnified ------------------------------------------------------------------------------ --- | This type family is used by 'Control.Lens.Zoom.Magnify' to describe the common effect type.+-- | This type family is used by 'Magnify' to describe the common effect type. type family Magnified (m :: * -> *) :: * -> * -> * type instance Magnified (ReaderT b m) = Effect m type instance Magnified ((->)b) = Const@@ -411,8 +411,8 @@ @ getBase :: 'Reader.Reader' Config String getBase = do-  protocol \<- 'Data.Maybe.fromMaybe' \"https\" '<$>' 'view' (base.protocol)-  path     \<- 'view' (base.path)+  protocol \<- 'Data.Maybe.fromMaybe' \"https\" '<$>' 'Lens.Micro.Mtl.view' (base.protocol)+  path     \<- 'Lens.Micro.Mtl.view' (base.path)   return (protocol ++ path) @ @@ -420,8 +420,8 @@  @ getBase = 'magnify' base $ do-  protocol \<- 'Data.Maybe.fromMaybe' \"https\" '<$>' 'view' protocol-  path     \<- 'view' path+  protocol \<- 'Data.Maybe.fromMaybe' \"https\" '<$>' 'Lens.Micro.Mtl.view' protocol+  path     \<- 'Lens.Micro.Mtl.view' path   return (protocol ++ path) @   -}@@ -487,4 +487,3 @@   {-# INLINE pure #-}   EffectRWS m <*> EffectRWS n = EffectRWS $ \st -> m st >>= \ (s,t,w) -> n t >>= \ (s',u,w') -> return (mappend s s', u, mappend w w')   {-# INLINE (<*>) #-}-