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

Safe HaskellNone
LanguageHaskell2010

Data.Lens.Light

Contents

Synopsis

Lenses and basic operations

newtype Lens a b #

Simple lens data type

Constructors

Lens 

Fields

Instances

Category * Lens # 

Methods

id :: cat a a #

(.) :: cat b c -> cat a b -> cat a c #

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

Build a lens out of a getter and setter

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

Build a lens out of an isomorphism

getL :: Lens a b -> a -> b #

Get the getter function from a lens

setL :: Lens a b -> b -> a -> a #

Get the setter function from a lens

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

Get the modifier function from a lens

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

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

(^.) :: b -> Lens b c -> c infixl 9 #

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

vanLaarhoven :: Functor f => Lens a b -> (b -> f b) -> a -> f a #

Convert a lens to its van Laarhoven representation

Generate lenses using TH

nameMakeLens :: Name -> (String -> Maybe String) -> Q [Dec] #

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] #

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] #

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

MonadState operators

access :: MonadState a m => Lens a b -> m b #

Get the value of a lens into state

(~=) :: MonadState a m => Lens a b -> b -> m () infixr 4 #

Set a value using a lens into state

(!=) :: MonadState a m => Lens a b -> b -> m () infixr 4 #

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 () infixr 4 #

Infix modification of a value through a lens into state

(!%=) :: MonadState a m => Lens a b -> (b -> b) -> m () infixr 4 #

Infix modification of a value through a lens into state. Forces both the function application and the whole state.

zoom :: (MonadStateT stateT, MonadState s (stateT s m), MonadTrans (stateT s), Monad m) => Lens s s' -> stateT s' m a -> stateT s m a #

Run a stateful computation with a smaller state inside another computation with a bigger state.

class MonadStateT t #

The purpose of this class is to abstract the difference between the lazy and strict state monads, so that zoom can work with either of them.

Minimal complete definition

runStateT

Instances

MonadStateT StateT # 

Methods

runStateT :: StateT s m a -> s -> m (a, s)

MonadStateT StateT # 

Methods

runStateT :: StateT s m a -> s -> m (a, s)