This is the main module for end-users of lens-families.
If you are not building your own lenses, but just using top-level defined lenses made by others, this is the only module you need.
^. for accessing fields and
%~ for setting and modifying fields.
Lenses are composed with
. from the
id is the identity lens.
Warning: Lenses are composed in the opposite order than most lens packages. Lenses in this library enjoy the following identities.
x ^. l1 . l2 === x ^. l1 ^. l2
l1 . l2 %~ f === l1 %~ l2 %~ f
The identity lens behaves as follows.
x ^. id === x
id %~ f === f
Remember that lenses and lens families can be used directly for functorial updates.
sndL id gives you strength.
sndL id :: Functor f => (a, f b) -> f (a, b)
Here is an example of code that uses the
Maybe functor to preserves sharing during update when possible.
-- | 'sharedUpdate' returns the *identical* object if the update doesn't change anything. -- This is useful for preserving sharing. sharedUpdate :: Eq b => Ref Maybe a b -> (b -> b) -> a -> a sharedUpdate lens f a = fromMaybe a (lens f' a) where f' b | fb == b = Nothing | otherwise = Just fb where fb = f b
For stock lenses, see Lens.Family.Stock.
To build your own lenses, see Lens.Family.Unchecked.
- getting :: (a -> b) -> GetterFamily a a' b b'
- project :: GetterFamily a a' b b' -> a -> b
- (^.) :: a -> GetterFamily a a' b b' -> b
- sec :: SetterFamily a a' b b' -> (b -> b') -> a -> a'
- (%~) :: SetterFamily a a' b b' -> (b -> b') -> a -> a'
- (<~) :: SetterFamily a a' b b' -> b' -> a -> a'
- (+~), (*~), (-~) :: Num b => Setter a b -> b -> a -> a
- (/~) :: Fractional b => Setter a b -> b -> a -> a
- (&&~), (||~) :: Setter a Bool -> Bool -> a -> a
- (<>~) :: Monoid o => Setter a o -> o -> a -> a
- type GetterFamily a a' b b' = RefFamily (Getting b) a a' b b'
- type Getter a b = GetterFamily a a b b
- type SetterFamily a a' b b' = RefFamily Setting a a' b b'
- type Setter a b = SetterFamily a a b b
getting promotes a projection function to a read-only lens.
To demote a lens to a projection function, use the section
(^. l) or
(3 :+ 4, "example") ^. fstL . getting abs5.0 :+ 0.0
Demote a setter to a semantic editor combinator.