| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Data.Vinyl.Optic.Tagged.Proxy.Identity
Description
This module provides accessors and modifiers for labeled records. As the module name indicates, the functions provided by this module are designed to work with records under the following conditions:
- The
TaggedFunctoris parameterized byIdentity. - The
VisibleTypeApplicationextension is unavailable.
- lens :: forall k g rs i v proxy. (Functor g, IxElem k rs i v) => proxy k -> (v -> g v) -> Rec (TaggedFunctor Identity) rs -> g (Rec (TaggedFunctor Identity) rs)
- get :: forall k rs i v proxy. IxElem k rs i v => proxy k -> Rec (TaggedFunctor Identity) rs -> v
- set :: forall k rs i v proxy. IxElem k rs i v => proxy k -> v -> Rec (TaggedFunctor Identity) rs -> Rec (TaggedFunctor Identity) rs
- modify :: forall k rs i v proxy. IxElem k rs i v => proxy k -> (v -> v) -> Rec (TaggedFunctor Identity) rs -> Rec (TaggedFunctor Identity) rs
Functions
lens :: forall k g rs i v proxy. (Functor g, IxElem k rs i v) => proxy k -> (v -> g v) -> Rec (TaggedFunctor Identity) rs -> g (Rec (TaggedFunctor Identity) rs) Source
get :: forall k rs i v proxy. IxElem k rs i v => proxy k -> Rec (TaggedFunctor Identity) rs -> v Source
set :: forall k rs i v proxy. IxElem k rs i v => proxy k -> v -> Rec (TaggedFunctor Identity) rs -> Rec (TaggedFunctor Identity) rs Source
modify :: forall k rs i v proxy. IxElem k rs i v => proxy k -> (v -> v) -> Rec (TaggedFunctor Identity) rs -> Rec (TaggedFunctor Identity) rs Source
Tutorial
Here is a explanation of how the functions in this module can be used. First we will create a record:
>>>import Data.Tagged.Functor>>>import Data.Functor.Identity>>>import Data.Proxy>>>import Data.Vinyl.Core>>>:{let person = tagIdentity (Proxy :: Proxy "age") (44 :: Int) :& tagIdentity (Proxy :: Proxy "name") ("Alexa" :: String) :& tagIdentity (Proxy :: Proxy "alive") True :& RNil :}
Notice that the type of person is inferred and fully monomorphic:
>>>:t personperson :: Rec (TaggedFunctor Identity) '['("age", Int), '("name", String), '("alive", Bool)]
The Identity wrappers are cumbersome to deal with. This module
provides extra functions (suffixed with Id) that get eliminate some
of the boilerplate. With these functions, the above becomes:
>>>get (Proxy :: Proxy "name") person"Alexa">>>let deceased2 = set (Proxy :: Proxy "alive") False person>>>get (Proxy :: Proxy "alive") deceased2False>>>let older2 = modify (Proxy :: Proxy "age") (+12) person>>>get (Proxy :: Proxy "age") older256