data-lens-light-0.1: Simple lenses, minimum dependencies

Safe HaskellNone

Data.Lens.Light

Contents

Synopsis

Lenses and basic operations

newtype Lens a b Source

Simple lens data type

Constructors

Lens 

Fields

runLens :: a -> (b -> a, b)
 

Instances

lens :: (a -> b) -> (b -> a -> a) -> Lens a bSource

Build a lens out of a getter and setter

iso :: (a -> b) -> (b -> a) -> Lens a bSource

Build a lens out of an isomorphism

getL :: Lens a b -> a -> bSource

Get the getter function from a lens

setL :: Lens a b -> b -> a -> aSource

Get the setter function from a lens

modL :: Lens a b -> (b -> b) -> a -> aSource

Get the modifier function from a lens

modL' :: Lens a b -> (b -> b) -> a -> aSource

Get the modifier function from a lens. Forces function application.

(^.) :: b -> Lens b c -> cSource

Infix version of getL (with the reverse order of the arguments)

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.

makeLens :: Name -> Q [Dec]Source

 makeLens a = makeLenses [a]
 $( makeLens ''TypeName )

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.