| Portability | Rank2Types | 
|---|---|
| Stability | provisional | 
| Maintainer | Edward Kmett <ekmett@gmail.com> | 
| Safe Haskell | Safe-Infered | 
Control.Lens.Setter
Contents
Description
A Setter a b c dfmap from Functor. It allows you to map into a
 structure and change out the contents, but it isn't strong enough to allow you to
 enumerate those contents. Starting with fmap :: 
 we monomorphize the type to obtain Functor f => (c -> d) -> f c -> f d(c -> d) -> a -> b and then decorate it with Identity to obtain
typeSettera b c d = (c ->Identityd) -> a ->Identityb
Every Traversal is a valid Setter, since Identity is Applicative.
Everything you can do with a Functor, you can do with a Setter. There
 are combinators that generalize fmap and (<$).
- type Setter a b c d = forall f. Settable f => (c -> f d) -> a -> f b
- class Applicative f => Settable f  where- untainted :: f a -> a
 
- sets :: ((c -> d) -> a -> b) -> Setter a b c d
- mapped :: Functor f => Setter (f a) (f b) a b
- over :: Setting a b c d -> (c -> d) -> a -> b
- mapOf :: Setting a b c d -> (c -> d) -> a -> b
- set :: Setting a b c d -> d -> a -> b
- (.~) :: Setting a b c d -> d -> a -> b
- (%~) :: Setting a b c d -> (c -> d) -> a -> b
- (+~) :: Num c => Setting a b c c -> c -> a -> b
- (-~) :: Num c => Setting a b c c -> c -> a -> b
- (*~) :: Num c => Setting a b c c -> c -> a -> b
- (//~) :: Fractional c => Setting a b c c -> c -> a -> b
- (^~) :: (Num c, Integral e) => Setting a b c c -> e -> a -> b
- (^^~) :: (Fractional c, Integral e) => Setting a b c c -> e -> a -> b
- (**~) :: Floating c => Setting a b c c -> c -> a -> b
- (||~) :: Setting a b Bool Bool -> Bool -> a -> b
- (&&~) :: Setting a b Bool Bool -> Bool -> a -> b
- (<.~) :: Setting a b c d -> d -> a -> (d, b)
- (.=) :: MonadState a m => Setting a a c d -> d -> m ()
- (%=) :: MonadState a m => Setting a a c d -> (c -> d) -> m ()
- (+=) :: (MonadState a m, Num b) => SimpleSetting a b -> b -> m ()
- (-=) :: (MonadState a m, Num b) => SimpleSetting a b -> b -> m ()
- (*=) :: (MonadState a m, Num b) => SimpleSetting a b -> b -> m ()
- (//=) :: (MonadState a m, Fractional b) => SimpleSetting a b -> b -> m ()
- (^=) :: (MonadState a m, Fractional b, Integral c) => SimpleSetting a b -> c -> m ()
- (^^=) :: (MonadState a m, Fractional b, Integral c) => SimpleSetting a b -> c -> m ()
- (**=) :: (MonadState a m, Floating b) => SimpleSetting a b -> b -> m ()
- (||=) :: MonadState a m => SimpleSetting a Bool -> Bool -> m ()
- (&&=) :: MonadState a m => SimpleSetting a Bool -> Bool -> m ()
- (<.=) :: MonadState a m => Setting a a c d -> d -> m d
- (<~) :: MonadState a m => Setting a a c d -> m d -> m ()
- type Setting a b c d = (c -> Mutator d) -> a -> Mutator b
- newtype  Mutator a = Mutator {- runMutator :: a
 
- type SimpleSetting a b = Setting a a b b
- type SimpleSetter a b = Setter a a b b
Setters
type Setter a b c d = forall f. Settable f => (c -> f d) -> a -> f bSource
The only Lens-like law that can apply to a Setter l is that
setl c (setl b a) =setl c a
You can't view a Setter in general, so the other two laws are irrelevant.
However, two Functor laws apply to a Setter:
overlid=idoverl f .overl g =overl (f . g)
These an be stated more directly:
lpure=purel f .untainted. l g = l (f .untainted. g)
You can compose a Setter with a Lens or a Traversal using (.) from the Prelude
 and the result is always only a Setter and nothing more.
class Applicative f => Settable f whereSource
Building Setters
Common Setters
Functional Combinators
over :: Setting a b c d -> (c -> d) -> a -> bSource
Modify the target of a Lens or all the targets of a Setter or Traversal
 with a function.
fmap=overmappedfmapDefault=overtraversesets.over=idover.sets=id
Another way to view over is to say that it transformers a Setter into a
 "semantic editor combinator".
over::Settera b c d -> (c -> d) -> a -> b
mapOf :: Setting a b c d -> (c -> d) -> a -> bSource
Modify the target of a Lens or all the targets of a Setter or Traversal
 with a function. This is an alias for over that is provided for consistency.
mapOf=overfmap=mapOfmappedfmapDefault=mapOftraversesets.mapOf=idmapOf.sets=id
mapOf ::Settera b c d -> (c -> d) -> a -> b mapOf ::Isoa b c d -> (c -> d) -> a -> b mapOf ::Lensa b c d -> (c -> d) -> a -> b mapOf ::Traversala b c d -> (c -> d) -> a -> b
set :: Setting a b c d -> d -> a -> bSource
Replace the target of a Lens or all of the targets of a Setter
 or Traversal with a constant value.
(<$) =setmapped
>>>import Control.Lens>>>set _2 "hello" (1,())(1,"hello")
>>>set mapped () [1,2,3,4][(),(),(),()]
Note: Attempting to set a Fold or Getter will fail at compile time with an
 relatively nice error message.
set ::Settera b c d -> d -> a -> b set ::Isoa b c d -> d -> a -> b set ::Lensa b c d -> d -> a -> b set ::Traversala b c d -> d -> a -> b
(.~) :: Setting a b c d -> d -> a -> bSource
Replace the target of a Lens or all of the targets of a Setter
 or Traversal with a constant value.
This is an infix version of set, provided for consistency with (.=)
f<$a =mapped.~f$a
>>>import Control.Lens>>>_1 .~ "hello" $ (42,"world")("hello","world")
(.~) ::Settera b c d -> d -> a -> b (.~) ::Isoa b c d -> d -> a -> b (.~) ::Lensa b c d -> d -> a -> b (.~) ::Traversala b c d -> d -> a -> b
(%~) :: Setting a b c d -> (c -> d) -> a -> bSource
Modifies the target of a Lens or all of the targets of a Setter or
 Traversal with a user supplied function.
This is an infix version of over
fmapf =mapped%~ffmapDefaultf =traverse%~f
>>>import Control.Lens>>>_2 %~ length $ (1,"hello")(1,5)
(%~) ::Settera b c d -> (c -> d) -> a -> b (%~) ::Isoa b c d -> (c -> d) -> a -> b (%~) ::Lensa b c d -> (c -> d) -> a -> b (%~) ::Traversala b c d -> (c -> d) -> a -> b
(+~) :: Num c => Setting a b c c -> c -> a -> bSource
Increment the target(s) of a numerically valued Lens, Setter or Traversal
>>>import Control.Lens>>>_1 +~ 1 $ (1,2)(2,2)
(+~) :: Num c =>Settera b c c -> c -> a -> b (+~) :: Num c =>Isoa b c c -> c -> a -> b (+~) :: Num c =>Lensa b c c -> c -> a -> b (+~) :: Num c =>Traversala b c c -> c -> a -> b
(-~) :: Num c => Setting a b c c -> c -> a -> bSource
Decrement the target(s) of a numerically valued Lens, Iso, Setter or Traversal
>>>import Control.Lens>>>_1 -~ 2 $ (1,2)(-1,2)
(-~) ::Numc =>Settera b c c -> c -> a -> b (-~) ::Numc =>Isoa b c c -> c -> a -> b (-~) ::Numc =>Lensa b c c -> c -> a -> b (-~) ::Numc =>Traversala b c c -> c -> a -> b
(*~) :: Num c => Setting a b c c -> c -> a -> bSource
Multiply the target(s) of a numerically valued Lens, Iso, Setter or Traversal
>>>import Control.Lens>>>_2 *~ 4 $ (1,2)(1,8)
(*~) ::Numc =>Settera b c c -> c -> a -> b (*~) ::Numc =>Isoa b c c -> c -> a -> b (*~) ::Numc =>Lensa b c c -> c -> a -> b (*~) ::Numc =>Traversala b c c -> c -> a -> b
(//~) :: Fractional c => Setting a b c c -> c -> a -> bSource
Divide the target(s) of a numerically valued Lens, Iso, Setter or Traversal
(~) ::Fractionalc =>Settera b c c -> c -> a -> b (~) ::Fractionalc =>Isoa b c c -> c -> a -> b (~) ::Fractionalc =>Lensa b c c -> c -> a -> b (~) ::Fractionalc =>Traversala b c c -> c -> a -> b
(^^~) :: (Fractional c, Integral e) => Setting a b c c -> e -> a -> bSource
Raise the target(s) of a fractionally valued Lens, Setter or Traversal to an integral power
>>>import Control.Lens>>>_2 ^^~ (-1) $ (1,2)(1,0.5)
(^^~) :: (Fractionalc,Integrale) =>Settera b c c -> e -> a -> b (^^~) :: (Fractionalc,Integrale) =>Isoa b c c -> e -> a -> b (^^~) :: (Fractionalc,Integrale) =>Lensa b c c -> e -> a -> b (^^~) :: (Fractionalc,Integrale) =>Traversala b c c -> e -> a -> b
(**~) :: Floating c => Setting a b c c -> c -> a -> bSource
Raise the target(s) of a floating-point valued Lens, Setter or Traversal to an arbitrary power.
>>>import Control.Lens>>>_2 **~ pi $ (1,3)(1,31.54428070019754)
(**~) ::Floatingc =>Settera b c c -> c -> a -> b (**~) ::Floatingc =>Isoa b c c -> c -> a -> b (**~) ::Floatingc =>Lensa b c c -> c -> a -> b (**~) ::Floatingc =>Traversala b c c -> c -> a -> b
(||~) :: Setting a b Bool Bool -> Bool -> a -> bSource
Logically || the target(s) of a Bool-valued Lens or Setter
>>>:m + Control.Lens Data.Pair.Lens
>>>both ||~ True $ (False,True)(True,True)
>>>both ||~ False $ (False,True)(False,True)
(||~)::Settera bBoolBool->Bool-> a -> b (||~)::Isoa bBoolBool->Bool-> a -> b (||~)::Lensa bBoolBool->Bool-> a -> b (||~)::Traversala bBoolBool->Bool-> a -> b
(&&~) :: Setting a b Bool Bool -> Bool -> a -> bSource
Logically && the target(s) of a Bool-valued Lens or Setter
>>>:m + Control.Lens Data.Pair.Lens
>>>both &&~ True $ (False, True)(False,True)
>>>both &&~ False $ (False, True)(False,False)
(&&~)::Settera bBoolBool->Bool-> a -> b (&&~)::Isoa bBoolBool->Bool-> a -> b (&&~)::Lensa bBoolBool->Bool-> a -> b (&&~)::Traversala bBoolBool->Bool-> a -> b
(<.~) :: Setting a b c d -> d -> a -> (d, b)Source
Set with pass-through
This is mostly present for consistency, but may be useful for for chaining assignments
If you do not need a copy of the intermediate result, then using l  directly is a good idea.
.~ d
(<.~) ::Settera b c d -> d -> a -> (d, b) (<.~) ::Isoa b c d -> d -> a -> (d, b) (<.~) ::Lensa b c d -> d -> a -> (d, b) (<.~) ::Traversala b c d -> d -> a -> (d, b)
State Combinators
(.=) :: MonadState a m => Setting a a c d -> d -> m ()Source
Replace the target of a Lens or all of the targets of a Setter or Traversal in our monadic
 state with a new value, irrespective of the old.
(.=) ::MonadStatea m =>Isoa a c d -> d -> m () (.=) ::MonadStatea m =>Lensa a c d -> d -> m () (.=) ::MonadStatea m =>Traversala a c d -> d -> m () (.=) ::MonadStatea m =>Settera a c d -> d -> m ()
(%=) :: MonadState a m => Setting a a c d -> (c -> d) -> m ()Source
Map over the target of a Lens or all of the targets of a Setter or Traversal in our monadic state.
(%=) ::MonadStatea m =>Isoa a c d -> (c -> d) -> m () (%=) ::MonadStatea m =>Lensa a c d -> (c -> d) -> m () (%=) ::MonadStatea m =>Traversala a c d -> (c -> d) -> m () (%=) ::MonadStatea m =>Settera a c d -> (c -> d) -> m ()
(+=) :: (MonadState a m, Num b) => SimpleSetting a b -> b -> m ()Source
Modify the target(s) of a Simple Lens, Iso, Setter or Traversal by adding a value
Example:
fresh :: MonadState Int m => m Int fresh = doid+=1useid
(+=) :: (MonadStatea m,Numb) =>SimpleSettera b -> b -> m () (+=) :: (MonadStatea m,Numb) =>SimpleIsoa b -> b -> m () (+=) :: (MonadStatea m,Numb) =>SimpleLensa b -> b -> m () (+=) :: (MonadStatea m,Numb) =>SimpleTraversala b -> b -> m ()
(-=) :: (MonadState a m, Num b) => SimpleSetting a b -> b -> m ()Source
Modify the target(s) of a Simple Lens, Iso, Setter or Traversal by subtracting a value
(-=) :: (MonadStatea m,Numb) =>SimpleSettera b -> b -> m () (-=) :: (MonadStatea m,Numb) =>SimpleIsoa b -> b -> m () (-=) :: (MonadStatea m,Numb) =>SimpleLensa b -> b -> m () (-=) :: (MonadStatea m,Numb) =>SimpleTraversala b -> b -> m ()
(*=) :: (MonadState a m, Num b) => SimpleSetting a b -> b -> m ()Source
Modify the target(s) of a Simple Lens, Iso, Setter or Traversal by multiplying by value.
ballSpeed.both*=speedMultiplier
(*=) :: (MonadStatea m,Numb) =>SimpleSettera b -> b -> m () (*=) :: (MonadStatea m,Numb) =>SimpleIsoa b -> b -> m () (*=) :: (MonadStatea m,Numb) =>SimpleLensa b -> b -> m () (*=) :: (MonadStatea m,Numb) =>SimpleTraversala b -> b -> m ()
(//=) :: (MonadState a m, Fractional b) => SimpleSetting a b -> b -> m ()Source
Modify the target(s) of a Simple Lens, Iso, Setter or Traversal by dividing by a value.
(=) :: (MonadStatea m,Fractionalb) =>SimpleSettera b -> b -> m () (=) :: (MonadStatea m,Fractionalb) =>SimpleIsoa b -> b -> m () (=) :: (MonadStatea m,Fractionalb) =>SimpleLensa b -> b -> m () (=) :: (MonadStatea m,Fractionalb) =>SimpleTraversala b -> b -> m ()
(^=) :: (MonadState a m, Fractional b, Integral c) => SimpleSetting a b -> c -> m ()Source
Raise the target(s) of a numerically valued Lens, Setter or Traversal to a non-negative integral power.
(^=) :: (MonadStatea m,Fractionalb,Integralc) =>SimpleSettera b -> c -> m () (^=) :: (MonadStatea m,Fractionalb,Integralc) =>SimpleIsoa b -> c -> m () (^=) :: (MonadStatea m,Fractionalb,Integralc) =>SimpleLensa b -> c -> m () (^=) :: (MonadStatea m,Fractionalb,Integralc) =>SimpleTraversala b -> c -> m ()
(^^=) :: (MonadState a m, Fractional b, Integral c) => SimpleSetting a b -> c -> m ()Source
Raise the target(s) of a numerically valued Lens, Setter or Traversal to an integral power.
(^^=) :: (MonadStatea m,Fractionalb,Integralc) =>SimpleSettera b -> c -> m () (^^=) :: (MonadStatea m,Fractionalb,Integralc) =>SimpleIsoa b -> c -> m () (^^=) :: (MonadStatea m,Fractionalb,Integralc) =>SimpleLensa b -> c -> m () (^^=) :: (MonadStatea m,Fractionalb,Integralc) =>SimpleTraversala b -> c -> m ()
(**=) :: (MonadState a m, Floating b) => SimpleSetting a b -> b -> m ()Source
Raise the target(s) of a numerically valued Lens, Setter or Traversal to an arbitrary power
(**=) :: (MonadStatea m,Floatingb) =>SimpleSettera b -> b -> m () (**=) :: (MonadStatea m,Floatingb) =>SimpleIsoa b -> b -> m () (**=) :: (MonadStatea m,Floatingb) =>SimpleLensa b -> b -> m () (**=) :: (MonadStatea m,Floatingb) =>SimpleTraversala b -> b -> m ()
(||=) :: MonadState a m => SimpleSetting a Bool -> Bool -> m ()Source
Modify the target(s) of a Simple Lens, 'Iso, Setter or Traversal by taking their logical || with a value
(||=)::MonadStatea m =>SimpleSetteraBool->Bool-> m () (||=)::MonadStatea m =>SimpleIsoaBool->Bool-> m () (||=)::MonadStatea m =>SimpleLensaBool->Bool-> m () (||=)::MonadStatea m =>SimpleTraversalaBool->Bool-> m ()
(&&=) :: MonadState a m => SimpleSetting a Bool -> Bool -> m ()Source
Modify the target(s) of a Simple Lens, Iso, Setter or Traversal by taking their logical && with a value
(&&=)::MonadStatea m =>SimpleSetteraBool->Bool-> m () (&&=)::MonadStatea m =>SimpleIsoaBool->Bool-> m () (&&=)::MonadStatea m =>SimpleLensaBool->Bool-> m () (&&=)::MonadStatea m =>SimpleTraversalaBool->Bool-> m ()
(<.=) :: MonadState a m => Setting a a c d -> d -> m dSource
Set with pass-through
This is useful for chaining assignment without round-tripping through your monad stack.
do x <- _2 <.= ninety_nine_bottles_of_beer_on_the_wallIf you do not need a copy of the intermediate result, then using l .= d will avoid unused binding warnings
(<.=) ::MonadStatea m =>Settera a c d -> d -> m d (<.=) ::MonadStatea m =>Isoa a c d -> d -> m d (<.=) ::MonadStatea m =>Lensa a c d -> d -> m d (<.=) ::MonadStatea m =>Traversala a c d -> d -> m d
(<~) :: MonadState a m => Setting a a c d -> m d -> m ()Source
Run a monadic action, and set all of the targets of a Lens, Setter or Traversal to its result.
(<~) ::MonadStatea m =>Isoa a c d -> m d -> m () (<~) ::MonadStatea m =>Lensa a c d -> m d -> m () (<~) ::MonadStatea m =>Traversala a c d -> m d -> m () (<~) ::MonadStatea m =>Settera a c d -> m d -> m ()
As a reasonable mnemonic, this lets you store the result of a monadic action in a lens rather than in a local variable.
 do foo <- bar
    ...
will store the result in a variable, while
 do foo <~ bar
    ...
Setter Internals
Mutator is just a renamed Identity functor to give better error
 messages when someone attempts to use a getter as a setter.
Most user code will never need to see this type.
Constructors
| Mutator | |
| Fields 
 | |
type SimpleSetting a b = Setting a a b bSource
This is a useful alias for use when consuming a SimpleSetter.
Most user code will never have to use this type.
typeSimpleSettingm =SimpleSetting
Simplicity
type SimpleSetter a b = Setter a a b bSource
A Simple Setter is just a Setter that doesn't change the types.
These are particularly common when talking about monomorphic containers. e.g.
setsData.Text.map ::SimpleSetterTextChar
typeSimpleSetter=SimpleSetter