Copyright | (C) 2012-16 Edward Kmett |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | Edward Kmett <ekmett@gmail.com> |
Stability | provisional |
Portability | Rank2Types |
Safe Haskell | Trustworthy |
Language | Haskell2010 |
A
is a purely functional reference.Lens
s t a b
While a Traversal
could be used for
Getting
like a valid Fold
, it
wasn't a valid Getter
as a
Getter
can't require an Applicative
constraint.
Functor
, however, is a constraint on both.
typeLens
s t a b = forall f.Functor
f => (a -> f b) -> s -> f t
Every Lens
can be used for Getting
like a
Fold
that doesn't use the Applicative
or
Functor
.
Every Lens
is a valid Traversal
that only uses
the Functor
part of the Applicative
it is supplied.
Every Lens
can be used for Getting
like a valid
Getter
.
Since every Lens
can be used for Getting
like a
valid Getter
it follows that it must view exactly one element in the
structure.
The Lens
laws follow from this property and the desire for it to act like
a Traversable
when used as a
Traversal
.
In the examples below, getter
and setter
are supplied as example getters
and setters, and are not actual functions supplied by this package.
Synopsis
- type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t
- type Lens' s a = Lens s s a a
- type ALens s t a b = LensLike (Pretext (->) a b) s t a b
- type ALens' s a = ALens s s a a
- lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b
- (%%~) :: LensLike f s t a b -> (a -> f b) -> s -> f t
- (%%=) :: MonadState s m => Over p ((,) r) s s a b -> p a (r, b) -> m r
- choosing :: Functor f => LensLike f s t a b -> LensLike f s' t' a b -> LensLike f (Either s s') (Either t t') a b
- alongside :: LensLike (AlongsideLeft f b') s t a b -> LensLike (AlongsideRight f t) s' t' a' b' -> LensLike f (s, s') (t, t') (a, a') (b, b')
- (<%~) :: LensLike ((,) b) s t a b -> (a -> b) -> s -> (b, t)
- (<+~) :: Num a => LensLike ((,) a) s t a a -> a -> s -> (a, t)
- (<-~) :: Num a => LensLike ((,) a) s t a a -> a -> s -> (a, t)
- (<*~) :: Num a => LensLike ((,) a) s t a a -> a -> s -> (a, t)
- (<//~) :: Fractional a => LensLike ((,) a) s t a a -> a -> s -> (a, t)
- (<^~) :: (Num a, Integral e) => LensLike ((,) a) s t a a -> e -> s -> (a, t)
- (<^^~) :: (Fractional a, Integral e) => LensLike ((,) a) s t a a -> e -> s -> (a, t)
- (<**~) :: Floating a => LensLike ((,) a) s t a a -> a -> s -> (a, t)
- (<||~) :: LensLike ((,) Bool) s t Bool Bool -> Bool -> s -> (Bool, t)
- (<&&~) :: LensLike ((,) Bool) s t Bool Bool -> Bool -> s -> (Bool, t)
- (<<>~) :: Semigroup m => LensLike ((,) m) s t m m -> m -> s -> (m, t)
- (<<%~) :: LensLike ((,) a) s t a b -> (a -> b) -> s -> (a, t)
- (<<.~) :: LensLike ((,) a) s t a b -> b -> s -> (a, t)
- (<<?~) :: LensLike ((,) a) s t a (Maybe b) -> b -> s -> (a, t)
- (<<+~) :: Num a => LensLike' ((,) a) s a -> a -> s -> (a, s)
- (<<-~) :: Num a => LensLike' ((,) a) s a -> a -> s -> (a, s)
- (<<*~) :: Num a => LensLike' ((,) a) s a -> a -> s -> (a, s)
- (<<//~) :: Fractional a => LensLike' ((,) a) s a -> a -> s -> (a, s)
- (<<^~) :: (Num a, Integral e) => LensLike' ((,) a) s a -> e -> s -> (a, s)
- (<<^^~) :: (Fractional a, Integral e) => LensLike' ((,) a) s a -> e -> s -> (a, s)
- (<<**~) :: Floating a => LensLike' ((,) a) s a -> a -> s -> (a, s)
- (<<||~) :: LensLike' ((,) Bool) s Bool -> Bool -> s -> (Bool, s)
- (<<&&~) :: LensLike' ((,) Bool) s Bool -> Bool -> s -> (Bool, s)
- (<<<>~) :: Semigroup r => LensLike' ((,) r) s r -> r -> s -> (r, s)
- (<%=) :: MonadState s m => LensLike ((,) b) s s a b -> (a -> b) -> m b
- (<+=) :: (MonadState s m, Num a) => LensLike' ((,) a) s a -> a -> m a
- (<-=) :: (MonadState s m, Num a) => LensLike' ((,) a) s a -> a -> m a
- (<*=) :: (MonadState s m, Num a) => LensLike' ((,) a) s a -> a -> m a
- (<//=) :: (MonadState s m, Fractional a) => LensLike' ((,) a) s a -> a -> m a
- (<^=) :: (MonadState s m, Num a, Integral e) => LensLike' ((,) a) s a -> e -> m a
- (<^^=) :: (MonadState s m, Fractional a, Integral e) => LensLike' ((,) a) s a -> e -> m a
- (<**=) :: (MonadState s m, Floating a) => LensLike' ((,) a) s a -> a -> m a
- (<||=) :: MonadState s m => LensLike' ((,) Bool) s Bool -> Bool -> m Bool
- (<&&=) :: MonadState s m => LensLike' ((,) Bool) s Bool -> Bool -> m Bool
- (<<>=) :: (MonadState s m, Semigroup r) => LensLike' ((,) r) s r -> r -> m r
- (<<%=) :: (forall a. Lift ((,) a) p, MonadState s m) => Over p ((,) a) s s a b -> p a b -> m a
- (<<.=) :: MonadState s m => LensLike ((,) a) s s a b -> b -> m a
- (<<?=) :: MonadState s m => LensLike ((,) a) s s a (Maybe b) -> b -> m a
- (<<+=) :: (MonadState s m, Num a) => LensLike' ((,) a) s a -> a -> m a
- (<<-=) :: (MonadState s m, Num a) => LensLike' ((,) a) s a -> a -> m a
- (<<*=) :: (MonadState s m, Num a) => LensLike' ((,) a) s a -> a -> m a
- (<<//=) :: (MonadState s m, Fractional a) => LensLike' ((,) a) s a -> a -> m a
- (<<^=) :: (MonadState s m, Num a, Integral e) => LensLike' ((,) a) s a -> e -> m a
- (<<^^=) :: (MonadState s m, Fractional a, Integral e) => LensLike' ((,) a) s a -> e -> m a
- (<<**=) :: (MonadState s m, Floating a) => LensLike' ((,) a) s a -> a -> m a
- (<<||=) :: MonadState s m => LensLike' ((,) Bool) s Bool -> Bool -> m Bool
- (<<&&=) :: MonadState s m => LensLike' ((,) Bool) s Bool -> Bool -> m Bool
- (<<<>=) :: (MonadState s m, Semigroup r) => LensLike' ((,) r) s r -> r -> m r
- devoid :: Over p f Void Void a b
- united :: Lens' a ()
- data Context a b t = Context (b -> t) a
- type Context' a = Context a a
Lenses
type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t Source #
A Lens
is actually a lens family as described in
http://comonad.com/reader/2012/mirrored-lenses/.
With great power comes great responsibility and a Lens
is subject to the
three common sense Lens
laws:
1) You get back what you put in:
view
l (set
l v s) ≡ v
2) Putting back what you got doesn't change anything:
set
l (view
l s) s ≡ s
3) Setting twice is the same as setting once:
set
l v' (set
l v s) ≡set
l v' s
These laws are strong enough that the 4 type parameters of a Lens
cannot
vary fully independently. For more on how they interact, read the "Why is
it a Lens Family?" section of
http://comonad.com/reader/2012/mirrored-lenses/.
There are some emergent properties of these laws:
1)
must be injective for every set
l ss
This is a consequence of law #1
2)
must be surjective, because of law #2, which indicates that it is possible to obtain any set
lv
from some s
such that set
s v = s
3) Given just the first two laws you can prove a weaker form of law #3 where the values v
that you are setting match:
set
l v (set
l v s) ≡set
l v s
Every Lens
can be used directly as a Setter
or Traversal
.
You can also use a Lens
for Getting
as if it were a
Fold
or Getter
.
Since every Lens
is a valid Traversal
, the
Traversal
laws are required of any Lens
you create:
lpure
≡pure
fmap
(l f).
l g ≡getCompose
.
l (Compose
.
fmap
f.
g)
typeLens
s t a b = forall f.Functor
f =>LensLike
f s t a b
Concrete Lenses
Combinators
(%%~) :: LensLike f s t a b -> (a -> f b) -> s -> f t infixr 4 Source #
(%%~
) can be used in one of two scenarios:
When applied to a Lens
, it can edit the target of the Lens
in a
structure, extracting a functorial result.
When applied to a Traversal
, it can edit the
targets of the traversals, extracting an applicative summary of its
actions.
>>>
[66,97,116,109,97,110] & each %%~ \a -> ("na", chr a)
("nananananana","Batman")
For all that the definition of this combinator is just:
(%%~
) ≡id
It may be beneficial to think about it as if it had these even more restricted types, however:
(%%~
) ::Functor
f =>Iso
s t a b -> (a -> f b) -> s -> f t (%%~
) ::Functor
f =>Lens
s t a b -> (a -> f b) -> s -> f t (%%~
) ::Applicative
f =>Traversal
s t a b -> (a -> f b) -> s -> f t
When applied to a Traversal
, it can edit the
targets of the traversals, extracting a supplemental monoidal summary
of its actions, by choosing f = ((,) m)
(%%~
) ::Iso
s t a b -> (a -> (r, b)) -> s -> (r, t) (%%~
) ::Lens
s t a b -> (a -> (r, b)) -> s -> (r, t) (%%~
) ::Monoid
m =>Traversal
s t a b -> (a -> (m, b)) -> s -> (m, t)
(%%=) :: MonadState s m => Over p ((,) r) s s a b -> p a (r, b) -> m r infix 4 Source #
Modify the target of a Lens
in the current state returning some extra
information of type r
or modify all targets of a
Traversal
in the current state, extracting extra
information of type r
and return a monoidal summary of the changes.
>>>
runState (_1 %%= \x -> (f x, g x)) (a,b)
(f a,(g a,b))
(%%=
) ≡ (state
.
)
It may be useful to think of (%%=
), instead, as having either of the
following more restricted type signatures:
(%%=
) ::MonadState
s m =>Iso
s s a b -> (a -> (r, b)) -> m r (%%=
) ::MonadState
s m =>Lens
s s a b -> (a -> (r, b)) -> m r (%%=
) :: (MonadState
s m,Monoid
r) =>Traversal
s s a b -> (a -> (r, b)) -> m r
Lateral Composition
choosing :: Functor f => LensLike f s t a b -> LensLike f s' t' a b -> LensLike f (Either s s') (Either t t') a b Source #
Merge two lenses, getters, setters, folds or traversals.
chosen
≡choosing
id
id
choosing
::Getter
s a ->Getter
s' a ->Getter
(Either
s s') achoosing
::Fold
s a ->Fold
s' a ->Fold
(Either
s s') achoosing
::Lens'
s a ->Lens'
s' a ->Lens'
(Either
s s') achoosing
::Traversal'
s a ->Traversal'
s' a ->Traversal'
(Either
s s') achoosing
::Setter'
s a ->Setter'
s' a ->Setter'
(Either
s s') a
alongside :: LensLike (AlongsideLeft f b') s t a b -> LensLike (AlongsideRight f t) s' t' a' b' -> LensLike f (s, s') (t, t') (a, a') (b, b') Source #
alongside
makes a Lens
from two other lenses or a Getter
from two other getters
by executing them on their respective halves of a product.
>>>
(Left a, Right b)^.alongside chosen chosen
(a,b)
>>>
(Left a, Right b) & alongside chosen chosen .~ (c,d)
(Left c,Right d)
alongside
::Lens
s t a b ->Lens
s' t' a' b' ->Lens
(s,s') (t,t') (a,a') (b,b')alongside
::Getter
s a ->Getter
s' a' ->Getter
(s,s') (a,a')
Setting Functionally with Passthrough
(<//~) :: Fractional a => LensLike ((,) a) s t a a -> a -> s -> (a, t) infixr 4 Source #
Divide the target of a fractionally valued Lens
and return the result.
When you do not need the result of the division, (//~
) is more flexible.
(<//~
) ::Fractional
a =>Lens'
s a -> a -> s -> (a, s) (<//~
) ::Fractional
a =>Iso'
s a -> a -> s -> (a, s)
(<^^~) :: (Fractional a, Integral e) => LensLike ((,) a) s t a a -> e -> s -> (a, t) infixr 4 Source #
Raise the target of a fractionally valued Lens
to an Integral
power
and return the result.
When you do not need the result of the operation, (^^~
) is more flexible.
(<^^~
) :: (Fractional
a,Integral
e) =>Lens'
s a -> e -> s -> (a, s) (<^^~
) :: (Fractional
a,Integral
e) =>Iso'
s a -> e -> s -> (a, s)
(<<?~) :: LensLike ((,) a) s t a (Maybe b) -> b -> s -> (a, t) infixr 4 Source #
Replace the target of a Lens
with a Just
value, but return the old value.
If you do not need the old value (?~
) is more flexible.
>>>
import Data.Map as Map
>>>
_2.at "hello" <<?~ "world" $ (42,Map.fromList [("goodnight","gracie")])
(Nothing,(42,fromList [("goodnight","gracie"),("hello","world")]))
(<<?~
) ::Iso
s t a (Maybe
b) -> b -> s -> (a, t) (<<?~
) ::Lens
s t a (Maybe
b) -> b -> s -> (a, t) (<<?~
) ::Traversal
s t a (Maybe
b) -> b -> s -> (a, t)
(<<+~) :: Num a => LensLike' ((,) a) s a -> a -> s -> (a, s) infixr 4 Source #
Increment the target of a numerically valued Lens
and return the old value.
When you do not need the old value, (+~
) is more flexible.
>>>
(a,b) & _1 <<+~ c
(a,(a + c,b))
>>>
(a,b) & _2 <<+~ c
(b,(a,b + c))
(<<+~
) ::Num
a =>Lens'
s a -> a -> s -> (a, s) (<<+~
) ::Num
a =>Iso'
s a -> a -> s -> (a, s)
(<<-~) :: Num a => LensLike' ((,) a) s a -> a -> s -> (a, s) infixr 4 Source #
Decrement the target of a numerically valued Lens
and return the old value.
When you do not need the old value, (-~
) is more flexible.
>>>
(a,b) & _1 <<-~ c
(a,(a - c,b))
>>>
(a,b) & _2 <<-~ c
(b,(a,b - c))
(<<-~
) ::Num
a =>Lens'
s a -> a -> s -> (a, s) (<<-~
) ::Num
a =>Iso'
s a -> a -> s -> (a, s)
(<<*~) :: Num a => LensLike' ((,) a) s a -> a -> s -> (a, s) infixr 4 Source #
Multiply the target of a numerically valued Lens
and return the old value.
When you do not need the old value, (-~
) is more flexible.
>>>
(a,b) & _1 <<*~ c
(a,(a * c,b))
>>>
(a,b) & _2 <<*~ c
(b,(a,b * c))
(<<*~
) ::Num
a =>Lens'
s a -> a -> s -> (a, s) (<<*~
) ::Num
a =>Iso'
s a -> a -> s -> (a, s)
(<<//~) :: Fractional a => LensLike' ((,) a) s a -> a -> s -> (a, s) infixr 4 Source #
Divide the target of a numerically valued Lens
and return the old value.
When you do not need the old value, (//~
) is more flexible.
>>>
(a,b) & _1 <<//~ c
(a,(a / c,b))
>>>
("Hawaii",10) & _2 <<//~ 2
(10.0,("Hawaii",5.0))
(<<//~
) :: Fractional a =>Lens'
s a -> a -> s -> (a, s) (<<//~
) :: Fractional a =>Iso'
s a -> a -> s -> (a, s)
(<<^^~) :: (Fractional a, Integral e) => LensLike' ((,) a) s a -> e -> s -> (a, s) infixr 4 Source #
Raise the target of a fractionally valued Lens
to an integral power and return the old value.
When you do not need the old value, (^^~
) is more flexible.
(<<^^~
) :: (Fractional
a,Integral
e) =>Lens'
s a -> e -> s -> (a, s) (<<^^~
) :: (Fractional
a,Integral
e) =>Iso'
s a -> e -> S -> (a, s)
(<<**~) :: Floating a => LensLike' ((,) a) s a -> a -> s -> (a, s) infixr 4 Source #
Raise the target of a floating-point valued Lens
to an arbitrary power and return the old value.
When you do not need the old value, (**~
) is more flexible.
>>>
(a,b) & _1 <<**~ c
(a,(a**c,b))
>>>
(a,b) & _2 <<**~ c
(b,(a,b**c))
(<<**~
) ::Floating
a =>Lens'
s a -> a -> s -> (a, s) (<<**~
) ::Floating
a =>Iso'
s a -> a -> s -> (a, s)
(<<||~) :: LensLike' ((,) Bool) s Bool -> Bool -> s -> (Bool, s) infixr 4 Source #
Logically ||
the target of a Bool
-valued Lens
and return the old value.
When you do not need the old value, (||~
) is more flexible.
>>>
(False,6) & _1 <<||~ True
(False,(True,6))
>>>
("hello",True) & _2 <<||~ False
(True,("hello",True))
(<<||~
) ::Lens'
sBool
->Bool
-> s -> (Bool
, s) (<<||~
) ::Iso'
sBool
->Bool
-> s -> (Bool
, s)
(<<&&~) :: LensLike' ((,) Bool) s Bool -> Bool -> s -> (Bool, s) infixr 4 Source #
Logically &&
the target of a Bool
-valued Lens
and return the old value.
When you do not need the old value, (&&~
) is more flexible.
>>>
(False,6) & _1 <<&&~ True
(False,(False,6))
>>>
("hello",True) & _2 <<&&~ False
(True,("hello",False))
(<<&&~
) ::Lens'
s Bool -> Bool -> s -> (Bool, s) (<<&&~
) ::Iso'
s Bool -> Bool -> s -> (Bool, s)
(<<<>~) :: Semigroup r => LensLike' ((,) r) s r -> r -> s -> (r, s) infixr 4 Source #
Modify the target of a monoidally valued Lens
by using (<>
) a new value and return the old value.
When you do not need the old value, (<>~
) is more flexible.
>>>
(Sum a,b) & _1 <<<>~ Sum c
(Sum {getSum = a},(Sum {getSum = a + c},b))
>>>
_2 <<<>~ ", 007" $ ("James", "Bond")
("Bond",("James","Bond, 007"))
(<<<>~
) ::Semigroup
r =>Lens'
s r -> r -> s -> (r, s) (<<<>~
) ::Semigroup
r =>Iso'
s r -> r -> s -> (r, s)
Setting State with Passthrough
(<%=) :: MonadState s m => LensLike ((,) b) s s a b -> (a -> b) -> m b infix 4 Source #
Modify the target of a Lens
into your Monad'
s state by a user supplied
function and return the result.
When applied to a Traversal
, it this will return a monoidal summary of all of the intermediate
results.
When you do not need the result of the operation, (%=
) is more flexible.
(<%=
) ::MonadState
s m =>Lens'
s a -> (a -> a) -> m a (<%=
) ::MonadState
s m =>Iso'
s a -> (a -> a) -> m a (<%=
) :: (MonadState
s m,Monoid
a) =>Traversal'
s a -> (a -> a) -> m a
(<+=) :: (MonadState s m, Num a) => LensLike' ((,) a) s a -> a -> m a infix 4 Source #
Add to the target of a numerically valued Lens
into your Monad'
s state
and return the result.
When you do not need the result of the addition, (+=
) is more
flexible.
(<+=
) :: (MonadState
s m,Num
a) =>Lens'
s a -> a -> m a (<+=
) :: (MonadState
s m,Num
a) =>Iso'
s a -> a -> m a
(<-=) :: (MonadState s m, Num a) => LensLike' ((,) a) s a -> a -> m a infix 4 Source #
Subtract from the target of a numerically valued Lens
into your Monad'
s
state and return the result.
When you do not need the result of the subtraction, (-=
) is more
flexible.
(<-=
) :: (MonadState
s m,Num
a) =>Lens'
s a -> a -> m a (<-=
) :: (MonadState
s m,Num
a) =>Iso'
s a -> a -> m a
(<*=) :: (MonadState s m, Num a) => LensLike' ((,) a) s a -> a -> m a infix 4 Source #
Multiply the target of a numerically valued Lens
into your Monad'
s
state and return the result.
When you do not need the result of the multiplication, (*=
) is more
flexible.
(<*=
) :: (MonadState
s m,Num
a) =>Lens'
s a -> a -> m a (<*=
) :: (MonadState
s m,Num
a) =>Iso'
s a -> a -> m a
(<//=) :: (MonadState s m, Fractional a) => LensLike' ((,) a) s a -> a -> m a infix 4 Source #
Divide the target of a fractionally valued Lens
into your Monad'
s state
and return the result.
When you do not need the result of the division, (//=
) is more flexible.
(<//=
) :: (MonadState
s m,Fractional
a) =>Lens'
s a -> a -> m a (<//=
) :: (MonadState
s m,Fractional
a) =>Iso'
s a -> a -> m a
(<^=) :: (MonadState s m, Num a, Integral e) => LensLike' ((,) a) s a -> e -> m a infix 4 Source #
Raise the target of a numerically valued Lens
into your Monad'
s state
to a non-negative Integral
power and return the result.
When you do not need the result of the operation, (^=
) is more flexible.
(<^=
) :: (MonadState
s m,Num
a,Integral
e) =>Lens'
s a -> e -> m a (<^=
) :: (MonadState
s m,Num
a,Integral
e) =>Iso'
s a -> e -> m a
(<^^=) :: (MonadState s m, Fractional a, Integral e) => LensLike' ((,) a) s a -> e -> m a infix 4 Source #
Raise the target of a fractionally valued Lens
into your Monad'
s state
to an Integral
power and return the result.
When you do not need the result of the operation, (^^=
) is more flexible.
(<^^=
) :: (MonadState
s m,Fractional
b,Integral
e) =>Lens'
s a -> e -> m a (<^^=
) :: (MonadState
s m,Fractional
b,Integral
e) =>Iso'
s a -> e -> m a
(<**=) :: (MonadState s m, Floating a) => LensLike' ((,) a) s a -> a -> m a infix 4 Source #
Raise the target of a floating-point valued Lens
into your Monad'
s
state to an arbitrary power and return the result.
When you do not need the result of the operation, (**=
) is more flexible.
(<**=
) :: (MonadState
s m,Floating
a) =>Lens'
s a -> a -> m a (<**=
) :: (MonadState
s m,Floating
a) =>Iso'
s a -> a -> m a
(<<%=) :: (forall a. Lift ((,) a) p, MonadState s m) => Over p ((,) a) s s a b -> p a b -> m a infix 4 Source #
Modify the target of a Lens
into your Monad'
s state by a user supplied
function and return the old value that was replaced.
When applied to a Traversal
, this will return a monoidal summary of all of the old values
present.
When you do not need the result of the operation, (%=
) is more flexible.
(<<%=
) ::MonadState
s m =>Lens'
s a -> (a -> a) -> m a (<<%=
) ::MonadState
s m =>Iso'
s a -> (a -> a) -> m a (<<%=
) :: (MonadState
s m,Monoid
a) =>Traversal'
s a -> (a -> a) -> m a
(<<%=
) ::MonadState
s m =>LensLike
((,)a) s s a b -> (a -> b) -> m a
(<<.=) :: MonadState s m => LensLike ((,) a) s s a b -> b -> m a infix 4 Source #
Replace the target of a Lens
into your Monad'
s state with a user supplied
value and return the old value that was replaced.
When applied to a Traversal
, this will return a monoidal summary of all of the old values
present.
When you do not need the result of the operation, (.=
) is more flexible.
(<<.=
) ::MonadState
s m =>Lens'
s a -> a -> m a (<<.=
) ::MonadState
s m =>Iso'
s a -> a -> m a (<<.=
) :: (MonadState
s m,Monoid
a) =>Traversal'
s a -> a -> m a
(<<?=) :: MonadState s m => LensLike ((,) a) s s a (Maybe b) -> b -> m a infix 4 Source #
Replace the target of a Lens
into your Monad'
s state with Just
a user supplied
value and return the old value that was replaced.
When applied to a Traversal
, this will return a monoidal summary of all of the old values
present.
When you do not need the result of the operation, (?=
) is more flexible.
(<<?=
) ::MonadState
s m =>Lens
s t a (Maybe b) -> b -> m a (<<?=
) ::MonadState
s m =>Iso
s t a (Maybe b) -> b -> m a (<<?=
) :: (MonadState
s m,Monoid
a) =>Traversal
s t a (Maybe b) -> b -> m a
(<<+=) :: (MonadState s m, Num a) => LensLike' ((,) a) s a -> a -> m a infix 4 Source #
Modify the target of a Lens
into your Monad'
s state by adding a value
and return the old value that was replaced.
When you do not need the result of the operation, (+=
) is more flexible.
(<<+=
) :: (MonadState
s m,Num
a) =>Lens'
s a -> a -> m a (<<+=
) :: (MonadState
s m,Num
a) =>Iso'
s a -> a -> m a
(<<-=) :: (MonadState s m, Num a) => LensLike' ((,) a) s a -> a -> m a infix 4 Source #
Modify the target of a Lens
into your Monad'
s state by subtracting a value
and return the old value that was replaced.
When you do not need the result of the operation, (-=
) is more flexible.
(<<-=
) :: (MonadState
s m,Num
a) =>Lens'
s a -> a -> m a (<<-=
) :: (MonadState
s m,Num
a) =>Iso'
s a -> a -> m a
(<<*=) :: (MonadState s m, Num a) => LensLike' ((,) a) s a -> a -> m a infix 4 Source #
Modify the target of a Lens
into your Monad'
s state by multipling a value
and return the old value that was replaced.
When you do not need the result of the operation, (*=
) is more flexible.
(<<*=
) :: (MonadState
s m,Num
a) =>Lens'
s a -> a -> m a (<<*=
) :: (MonadState
s m,Num
a) =>Iso'
s a -> a -> m a
(<<//=) :: (MonadState s m, Fractional a) => LensLike' ((,) a) s a -> a -> m a infix 4 Source #
Modify the target of a Lens
into your Monad
s state by dividing by a value
and return the old value that was replaced.
When you do not need the result of the operation, (//=
) is more flexible.
(<<//=
) :: (MonadState
s m,Fractional
a) =>Lens'
s a -> a -> m a (<<//=
) :: (MonadState
s m,Fractional
a) =>Iso'
s a -> a -> m a
(<<^=) :: (MonadState s m, Num a, Integral e) => LensLike' ((,) a) s a -> e -> m a infix 4 Source #
Modify the target of a Lens
into your Monad'
s state by raising it by a non-negative power
and return the old value that was replaced.
When you do not need the result of the operation, (^=
) is more flexible.
(<<^=
) :: (MonadState
s m,Num
a,Integral
e) =>Lens'
s a -> e -> m a (<<^=
) :: (MonadState
s m,Num
a,Integral
e) =>Iso'
s a -> a -> m a
(<<^^=) :: (MonadState s m, Fractional a, Integral e) => LensLike' ((,) a) s a -> e -> m a infix 4 Source #
Modify the target of a Lens
into your Monad'
s state by raising it by an integral power
and return the old value that was replaced.
When you do not need the result of the operation, (^^=
) is more flexible.
(<<^^=
) :: (MonadState
s m,Fractional
a,Integral
e) =>Lens'
s a -> e -> m a (<<^^=
) :: (MonadState
s m,Fractional
a,Integral
e) =>Iso'
s a -> e -> m a
(<<**=) :: (MonadState s m, Floating a) => LensLike' ((,) a) s a -> a -> m a infix 4 Source #
Modify the target of a Lens
into your Monad'
s state by raising it by an arbitrary power
and return the old value that was replaced.
When you do not need the result of the operation, (**=
) is more flexible.
(<<**=
) :: (MonadState
s m,Floating
a) =>Lens'
s a -> a -> m a (<<**=
) :: (MonadState
s m,Floating
a) =>Iso'
s a -> a -> m a
(<<||=) :: MonadState s m => LensLike' ((,) Bool) s Bool -> Bool -> m Bool infix 4 Source #
Modify the target of a Lens
into your Monad'
s state by taking its logical ||
with a value
and return the old value that was replaced.
When you do not need the result of the operation, (||=
) is more flexible.
(<<||=
) ::MonadState
s m =>Lens'
sBool
->Bool
-> mBool
(<<||=
) ::MonadState
s m =>Iso'
sBool
->Bool
-> mBool
(<<&&=) :: MonadState s m => LensLike' ((,) Bool) s Bool -> Bool -> m Bool infix 4 Source #
Modify the target of a Lens
into your Monad'
s state by taking its logical &&
with a value
and return the old value that was replaced.
When you do not need the result of the operation, (&&=
) is more flexible.
(<<&&=
) ::MonadState
s m =>Lens'
sBool
->Bool
-> mBool
(<<&&=
) ::MonadState
s m =>Iso'
sBool
->Bool
-> mBool
(<<<>=) :: (MonadState s m, Semigroup r) => LensLike' ((,) r) s r -> r -> m r infix 4 Source #
Modify the target of a Lens
into your Monad'
s state by using (<>
)
and return the old value that was replaced.
When you do not need the result of the operation, (<>=
) is more flexible.
(<<<>=
) :: (MonadState
s m,Semigroup
r) =>Lens'
s r -> r -> m r (<<<>=
) :: (MonadState
s m,Semigroup
r) =>Iso'
s r -> r -> m r
Common Lenses
We can always retrieve a ()
from any type.
>>>
"hello"^.united
()
>>>
"hello" & united .~ ()
"hello"
Context
The indexed store can be used to characterize a Lens
and is used by cloneLens
.
is isomorphic to
Context
a b tnewtype
,
and to Context
a b t = Context
{ runContext :: forall f. Functor
f => (a -> f b) -> f t }exists s. (s,
.Lens
s t a b)
A Context
is like a Lens
that has already been applied to a some structure.
Context (b -> t) a |