This module lets you define your own instances of Zoom and Magnify.
The warning from Lens.Micro.Internal applies to this module as well. Don't export functions that have Zoom or Magnify in their type signatures. If you absolutely need to define an instance (e.g. for internal use), only do it for your own types, because otherwise I might add an instance to one of the microlens packages later and if our instances are different it might lead to subtle bugs.
When you're in a state monad, this function lets you operate on a part of your state. For instance, if your state was a record containing a position field, after zooming position would become your whole state (and when you modify it, the bigger structure would be modified as well).
(Your State / StateT or RWS / RWST can be anywhere in the stack, but you can't use zoom with arbitrary MonadState because it doesn't provide any methods to change the type of the state. See this issue for details.)
For the sake of the example, let's define some types first:
data Position = Position {
_x, _y :: Int }
data Player = Player {
_position :: Position,
... }
data Game = Game {
_player :: Player,
_obstacles :: [Position],
... }
concat <$> mapM makeLenses [''Position, ''Player, ''Game]
Now, here's an action that moves the player north-east:
moveNE :: State Game ()
moveNE = do
player.position.x += 1
player.position.y += 1
With zoom, you can use player.position to focus just on a part of the state:
moveNE :: State Game ()
moveNE = do
zoom (player.position) $ do
x += 1
y += 1
You can just as well use it for retrieving things out of the state:
getCoords :: State Game (Int, Int)
getCoords = zoom (player.position) ((,) <$>use x <*>use y)
Or more explicitly:
getCoords = zoom (player.position) $ do
x' <- use x
y' <- use y
return (x', y')
When you pass a traversal to zoom, it'll work as a loop. For instance, here we move all obstacles:
moveObstaclesNE :: State Game ()
moveObstaclesNE = do
zoom (obstacles.each) $ do
x += 1
y += 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
-- Get old coordinates.
x' <- use x
y' <- use y
-- Update them.
x .= x' + 1
y .= y' + 1
-- Return a single-element list with old coordinates.
return [(x', y')]
...
Finally, you might need to write your own instances of Zoom if you use newtyped transformers in your monad stack. This can be done as follows:
import Lens.Micro.Mtl.Internal
type instance Zoomed (MyStateT s m) = Zoomed (StateT s m)
instance Monad m => Zoom (MyStateT s m) (MyStateT t m) s t where
zoom l (MyStateT m) = MyStateT (zoom l m)
Instances10Zoom, …
Zoommnst => Zoom (MaybeTm) (MaybeTn) stDefined in microlens-mtl-0.2.1.0 · Lens.Micro.Mtl.Internal
Monadz => Zoom (StateTsz) (StateTtz) stDefined in microlens-mtl-0.2.1.0 · Lens.Micro.Mtl.Internal
Monadz => Zoom (StateTsz) (StateTtz) stDefined in microlens-mtl-0.2.1.0 · Lens.Micro.Mtl.Internal
Zoommnst => Zoom (ExceptTem) (ExceptTen) stDefined in microlens-mtl-0.2.1.0 · Lens.Micro.Mtl.Internal
This is an equivalent of local which lets you apply a getter to your environment instead of merely applying a function (and it also lets you change the type of the environment).