Safe Haskell | None |
---|
- newtype Lens a b = Lens {
- runLens :: a -> (b -> a, b)
- lens :: (a -> b) -> (b -> a -> a) -> Lens a b
- iso :: (a -> b) -> (b -> a) -> Lens a b
- getL :: Lens a b -> a -> b
- setL :: Lens a b -> b -> a -> a
- modL :: Lens a b -> (b -> b) -> a -> a
- modL' :: Lens a b -> (b -> b) -> a -> a
- (^.) :: b -> Lens b c -> c
- nameMakeLens :: Name -> (String -> Maybe String) -> Q [Dec]
- makeLenses :: [Name] -> Q [Dec]
- makeLens :: Name -> Q [Dec]
- access :: MonadState a m => Lens a b -> m b
- (~=) :: MonadState a m => Lens a b -> b -> m ()
- (!=) :: MonadState a m => Lens a b -> b -> m ()
- (%=) :: MonadState a m => Lens a b -> (b -> b) -> m ()
- (!%=) :: MonadState a m => Lens a b -> (b -> b) -> m ()
Lenses and basic operations
modL' :: Lens a b -> (b -> b) -> a -> aSource
Get the modifier function from a lens. Forces function application.
Generate lenses using TH
nameMakeLens :: Name -> (String -> Maybe String) -> Q [Dec]Source
nameMakeLens n f
where n
is the name of a data type
declared with data
and f
is a function from names of fields
in that data type to the name of the corresponding accessor. If
f
returns Nothing
, then no accessor is generated for that
field.
makeLenses :: [Name] -> Q [Dec]Source
makeLenses n
where n
is the name of a data type
declared with data
looks through all the declared fields
of the data type, and for each field beginning with an underscore
generates an accessor of the same name without the underscore.
It is nameMakeLens n f where f
satisfies
f ('_' : s) = Just s f x = Nothing -- otherwise
For example, given the data type:
data Score = Score { _p1Score :: Int , _p2Score :: Int , rounds :: Int }
makeLenses
will generate the following objects:
p1Score :: Lens Score Int p1Score = lens _p1Score (\x s -> s { _p1Score = x }) p2Score :: Lens Score Int p2Score = lens _p2Score (\x s -> s { _p2Score = x })
It is used with Template Haskell syntax like:
$( makeLenses [''TypeName] )
And will generate accessors when TypeName was declared
using data
or newtype
.
MonadState operators
access :: MonadState a m => Lens a b -> m bSource
Get the value of a lens into state
(~=) :: MonadState a m => Lens a b -> b -> m ()Source
Set a value using a lens into state
(!=) :: MonadState a m => Lens a b -> b -> m ()Source
Set a value using a lens into state. Forces both the value and the whole state.
(%=) :: MonadState a m => Lens a b -> (b -> b) -> m ()Source
Infix modification of a value through a lens into state
(!%=) :: MonadState a m => Lens a b -> (b -> b) -> m ()Source
Infix modification of a value through a lens into state. Forces both the function application and the whole state.