data-accessor-0.2.3.1: Utilities for accessing and manipulating fields of records

Safe Haskell | Safe-Inferred
Language | Haskell98

Data.Accessor

Description

This module provides a simple abstract data type for a piece of a data stucture that can be read from and written to. In contrast to Data.Accessor.Basic it is intended for unqualified import.

Synopsis

- type Accessor r a = T r a
- accessor :: (r -> a) -> (a -> r -> r) -> Accessor r a
- setVal :: Accessor r a -> a -> r -> r
- (^=) :: T r a -> a -> r -> r
- getVal :: Accessor r a -> r -> a
- (^.) :: r -> T r a -> a
- (^:) :: T r a -> (a -> a) -> r -> r
- getA :: forall (m :: Type -> Type) r a. Monad m => Accessor r a -> StateT r m a
- putA :: forall (m :: Type -> Type) r a. Monad m => Accessor r a -> a -> StateT r m ()
- (=:) :: forall (m :: Type -> Type) r a. Monad m => Accessor r a -> a -> StateT r m ()
- (%=) :: forall (m :: Type -> Type) r a. Monad m => T r a -> a -> StateT r m ()
- modA :: forall (m :: Type -> Type) r a. Monad m => Accessor r a -> (a -> a) -> StateT r m ()
- (%:) :: forall (m :: Type -> Type) r a. Monad m => T r a -> (a -> a) -> StateT r m ()
- (.>) :: Accessor a b -> Accessor b c -> Accessor a c
- (<.) :: Accessor b c -> Accessor a b -> Accessor a c

# Documentation

type Accessor r a = T r a Source #

An `Accessor r a` is an object that encodes how to get and put a subject of type `a` out of/into an object of type `s`.

In order for an instance of this data structure `a` to be an `Accessor`, it must obey the following laws:

    getVal a (setVal a x r) = x
    setVal a (getVal a r) r = r

accessor Source #

Arguments

:: (r -> a) | get method
-> (a -> r -> r) | set method
-> Accessor r a |

Construct an `Accessor` from a `get` and a `set` method.

setVal Source #

Arguments

:: Accessor r a | record field `f`
-> a | value `x` to be set
-> r | original record
-> r | new record with field `f` changed to `x`

Set a value of a record field that is specified by an Accessor

(^=) :: T r a -> a -> r -> r infixr 5 Source #

`set` as infix operator. This lets us write `first ^= 2+3 $ second ^= 5+7 $ record`.

getVal Source #

Arguments

:: Accessor r a | record field
-> r | record
-> a | value of the field in the record

Get a value from a record field that is specified by an Accessor

(^.) :: r -> T r a -> a infixl 8 Source #

`get` as infix operator. This lets us write `record^.field^.subfield`. This imitates Modula II syntax.

(^:) :: T r a -> (a -> a) -> r -> r infixr 5 Source #

`modify` as infix operator. This lets us write `field^:subfield^:(2*) $ record`, `record$%field^:subfield^:(2*)` or `record$%field^:subfield^:(const 1)`.

getA :: forall (m :: Type -> Type) r a. Monad m => Accessor r a -> StateT r m a Source #

Deprecated: Data.Accessor.Monad.Trans.State.get from data-accessor-transformers package

A structural dereference function for state monads.

putA :: forall (m :: Type -> Type) r a. Monad m => Accessor r a -> a -> StateT r m () Source #

Deprecated: Data.Accessor.Monad.Trans.State.set from data-accessor-transformers package

A structural assignment function for state monads.

(=:) :: forall (m :: Type -> Type) r a. Monad m => Accessor r a -> a -> StateT r m () infix 1 Source #

Deprecated: use (Data.Accessor.Monad.Trans.State.%=) from data-accessor-transformers package

An "assignment operator" for state monads.

    (=:) = putA

(%=) :: forall (m :: Type -> Type) r a. Monad m => T r a -> a -> StateT r m () infix 1 Source #

Infix variant of `set`.

modA :: forall (m :: Type -> Type) r a. Monad m => Accessor r a -> (a -> a) -> StateT r m () Source #

Deprecated: Data.Accessor.Monad.Trans.State.modify from data-accessor-transformers package

A structural modification function for state monads.

(%:) :: forall (m :: Type -> Type) r a. Monad m => T r a -> (a -> a) -> StateT r m () infix 1 Source #

Infix variant of `modify`.

(.>) :: Accessor a b -> Accessor b c -> Accessor a c infixl 9 Source #

Accessor composition: Combine an accessor with an accessor to a sub-field. Speak "stack".

(<.) :: Accessor b c -> Accessor a b -> Accessor a c infixr 9 Source #

Accessor composition the other direction.

    (<.) = flip (.>)

You may also use the `(.)` operator from Category class.