| Safe Haskell | Safe | 
|---|---|
| Language | Haskell2010 | 
Optic.Getting
Contents
Synopsis
- type Getting r s a = (a -> Const r a) -> s -> Const r s
 - (^.) :: s -> Getting a s a -> a
 - view :: MonadReader s m => Getting a s a -> m a
 - views :: MonadReader s m => LensLike' (Const r :: * -> *) s a -> (a -> r) -> m r
 - use :: MonadState s m => Getting a s a -> m a
 - uses :: MonadState s m => LensLike' (Const r :: * -> *) s a -> (a -> r) -> m r
 
Getting
type Getting r s a = (a -> Const r a) -> s -> Const r s #
When you see this in a type signature it indicates that you can
 pass the function a Lens, Getter,
 Traversal, Fold,
 Prism, Iso, or one of
 the indexed variants, and it will just "do the right thing".
Most Getter combinators are able to be used with both a Getter or a
 Fold in limited situations, to do so, they need to be
 monomorphic in what we are going to extract with Const. To be compatible
 with Lens, Traversal and
 Iso we also restricted choices of the irrelevant t and
 b parameters.
If a function accepts a , then when Getting r s ar is a Monoid, then
 you can pass a Fold (or
 Traversal), otherwise you can only pass this a
 Getter or Lens.
(^.) :: s -> Getting a s a -> a infixl 8 #
View the value pointed to by a Getter or Lens or the
 result of folding over all the results of a Fold or
 Traversal that points at a monoidal values.
This is the same operation as view with the arguments flipped.
The fixity and semantics are such that subsequent field accesses can be
 performed with (.).
>>>(a,b)^._2b
>>>("hello","world")^._2"world"
>>>import Data.Complex>>>((0, 1 :+ 2), 3)^._1._2.to magnitude2.23606797749979
(^.) :: s ->Getters a -> a (^.) ::Monoidm => s ->Folds m -> m (^.) :: s ->Iso's a -> a (^.) :: s ->Lens's a -> a (^.) ::Monoidm => s ->Traversal's m -> m
view :: MonadReader s m => Getting a s a -> m a #
View the value pointed to by a Getter, Iso or
 Lens or the result of folding over all the results of a
 Fold or Traversal that points
 at a monoidal value.
view.to≡id
>>>view (to f) af a
>>>view _2 (1,"hello")"hello"
>>>view (to succ) 56
>>>view (_2._1) ("hello",("world","!!!"))"world"
As view is commonly used to access the target of a Getter or obtain a monoidal summary of the targets of a Fold,
 It may be useful to think of it as having one of these more restricted signatures:
view::Getters a -> s -> aview::Monoidm =>Folds m -> s -> mview::Iso's a -> s -> aview::Lens's a -> s -> aview::Monoidm =>Traversal's m -> s -> m
In a more general setting, such as when working with a Monad transformer stack you can use:
view::MonadReaders m =>Getters a -> m aview:: (MonadReaders m,Monoida) =>Folds a -> m aview::MonadReaders m =>Iso's a -> m aview::MonadReaders m =>Lens's a -> m aview:: (MonadReaders m,Monoida) =>Traversal's a -> m a
views :: MonadReader s m => LensLike' (Const r :: * -> *) s a -> (a -> r) -> m r #
View a function of the value pointed to by a Getter or Lens or the result of
 folding over the result of mapping the targets of a Fold or
 Traversal.
viewsl f ≡view(l.tof)
>>>views (to f) g ag (f a)
>>>views _2 length (1,"hello")5
As views is commonly used to access the target of a Getter or obtain a monoidal summary of the targets of a Fold,
 It may be useful to think of it as having one of these more restricted signatures:
views::Getters a -> (a -> r) -> s -> rviews::Monoidm =>Folds a -> (a -> m) -> s -> mviews::Iso's a -> (a -> r) -> s -> rviews::Lens's a -> (a -> r) -> s -> rviews::Monoidm =>Traversal's a -> (a -> m) -> s -> m
In a more general setting, such as when working with a Monad transformer stack you can use:
views::MonadReaders m =>Getters a -> (a -> r) -> m rviews:: (MonadReaders m,Monoidr) =>Folds a -> (a -> r) -> m rviews::MonadReaders m =>Iso's a -> (a -> r) -> m rviews::MonadReaders m =>Lens's a -> (a -> r) -> m rviews:: (MonadReaders m,Monoidr) =>Traversal's a -> (a -> r) -> m r
views::MonadReaders m =>Gettingr s a -> (a -> r) -> m r
use :: MonadState s m => Getting a s a -> m a #
Use the target of a Lens, Iso, or
 Getter in the current state, or use a summary of a
 Fold or Traversal that points
 to a monoidal value.
>>>evalState (use _1) (a,b)a
>>>evalState (use _1) ("hello","world")"hello"
use::MonadStates m =>Getters a -> m ause:: (MonadStates m,Monoidr) =>Folds r -> m ruse::MonadStates m =>Iso's a -> m ause::MonadStates m =>Lens's a -> m ause:: (MonadStates m,Monoidr) =>Traversal's r -> m r
uses :: MonadState s m => LensLike' (Const r :: * -> *) s a -> (a -> r) -> m r #
Use the target of a Lens, Iso or
 Getter in the current state, or use a summary of a
 Fold or Traversal that
 points to a monoidal value.
>>>evalState (uses _1 length) ("hello","world")5
uses::MonadStates m =>Getters a -> (a -> r) -> m ruses:: (MonadStates m,Monoidr) =>Folds a -> (a -> r) -> m ruses::MonadStates m =>Lens's a -> (a -> r) -> m ruses::MonadStates m =>Iso's a -> (a -> r) -> m ruses:: (MonadStates m,Monoidr) =>Traversal's a -> (a -> r) -> m r
uses::MonadStates m =>Gettingr s t a b -> (a -> r) -> m r