microlens-mtl-0.1.0.0: microlens support for Reader/Writer/State from mtl

Safe HaskellSafe-Inferred
LanguageHaskell2010

Lens.Micro.Mtl

Synopsis

Documentation

view :: MonadReader s m => Getting a s a -> m a Source

view is a synonym for (^.), generalised for MonadReader (since functions are instances of the MonadReader class).

>>> view _1 (1, 2)
1

It's often used when dealing with environment, for instance:

doSomething :: (MonadReader Config m) => m Int
doSomething = do
  thingy        <- view setting1  -- same as “asks (^. setting1)”
  anotherThingy <- view setting2
  ...

use :: MonadState s m => Getting a s a -> m a Source

use is view which implicitly operates on the state.

use l = gets (view l)

(.=) :: MonadState s m => ASetter s s a b -> b -> m () infix 4 Source

Assign value to the target. This is .~ which works in State.

l .= b = modify (l .~ b)

(%=) :: MonadState s m => ASetter s s a b -> (a -> b) -> m () infix 4 Source

Apply a function to the target. This is %~ which works in State.

>>> execState (do _1 %= (+1); _2 %= reverse) (1,"hello")
(2,"olleh")

(+=) :: (MonadState s m, Num a) => ASetter s s a a -> a -> m () infix 4 Source

Add a number to the target.

l += x = l %= (+x)

(-=) :: (MonadState s m, Num a) => ASetter s s a a -> a -> m () infix 4 Source

Subtract a number from the target.

l -= x = l %= (subtract x)

(*=) :: (MonadState s m, Num a) => ASetter s s a a -> a -> m () infix 4 Source

Multiply the target by a number.

l *= x = l %= (*x)

(//=) :: (MonadState s m, Fractional a) => ASetter s s a a -> a -> m () infix 4 Source

Divide the target by a number.

l /= x = l %= (x)