| Portability | Rank2Types |
|---|---|
| Stability | provisional |
| Maintainer | Edward Kmett <ekmett@gmail.com> |
| Safe Haskell | Safe-Inferred |
Control.Lens.Setter
Contents
Description
A is a generalization of Setter s t a bfmap 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 => (a -> b) -> f a -> f b(a -> b) -> s -> t and then decorate it with Identity to obtain
typeSetters t a b = (a ->Identityb) -> s ->Identityt
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 s t a b = forall f. Settable f => (a -> f b) -> s -> f t
- sets :: ((a -> b) -> s -> t) -> Setter s t a b
- mapped :: Functor f => Setter (f a) (f b) a b
- lifted :: Monad m => Setter (m a) (m b) a b
- over :: Setting s t a b -> (a -> b) -> s -> t
- mapOf :: Setting s t a b -> (a -> b) -> s -> t
- set :: Setting s t a b -> b -> s -> t
- (.~) :: Setting s t a b -> b -> s -> t
- (%~) :: Setting s t a b -> (a -> b) -> s -> t
- (+~) :: Num a => Setting s t a a -> a -> s -> t
- (-~) :: Num a => Setting s t a a -> a -> s -> t
- (*~) :: Num a => Setting s t a a -> a -> s -> t
- (//~) :: Fractional s => Setting a b s s -> s -> a -> b
- (^~) :: (Num a, Integral e) => Setting s t a a -> e -> s -> t
- (^^~) :: (Fractional a, Integral e) => Setting s t a a -> e -> s -> t
- (**~) :: Floating a => Setting s t a a -> a -> s -> t
- (||~) :: Setting s t Bool Bool -> Bool -> s -> t
- (<>~) :: Monoid a => Setting s t a a -> a -> s -> t
- (&&~) :: Setting s t Bool Bool -> Bool -> s -> t
- (<.~) :: Setting s t a b -> b -> s -> (b, t)
- (?~) :: Setting s t a (Maybe b) -> b -> s -> t
- (<?~) :: Setting s t a (Maybe b) -> b -> s -> (b, t)
- assign :: MonadState s m => Setting s s a b -> b -> m ()
- (.=) :: MonadState s m => Setting s s a b -> b -> m ()
- (%=) :: MonadState s m => Setting s s a b -> (a -> b) -> m ()
- (+=) :: (MonadState s m, Num a) => SimpleSetting s a -> a -> m ()
- (-=) :: (MonadState s m, Num a) => SimpleSetting s a -> a -> m ()
- (*=) :: (MonadState s m, Num a) => SimpleSetting s a -> a -> m ()
- (//=) :: (MonadState s m, Fractional a) => SimpleSetting s a -> a -> m ()
- (^=) :: (MonadState s m, Num a, Integral e) => SimpleSetting s a -> e -> m ()
- (^^=) :: (MonadState s m, Fractional a, Integral e) => SimpleSetting s a -> e -> m ()
- (**=) :: (MonadState s m, Floating a) => SimpleSetting s a -> a -> m ()
- (||=) :: MonadState s m => SimpleSetting s Bool -> Bool -> m ()
- (<>=) :: (MonadState s m, Monoid a) => SimpleSetting s a -> a -> m ()
- (&&=) :: MonadState s m => SimpleSetting s Bool -> Bool -> m ()
- (<.=) :: MonadState s m => Setting s s a b -> b -> m b
- (?=) :: MonadState s m => Setting s s a (Maybe b) -> b -> m ()
- (<?=) :: MonadState s m => Setting s s a (Maybe b) -> b -> m b
- (<~) :: MonadState s m => Setting s s a b -> m b -> m ()
- set' :: Setting s s a a -> a -> s -> s
- newtype ReifiedSetter s t a b = ReifySetter {
- reflectSetter :: Setter s t a b
- type Setting s t a b = (a -> Mutator b) -> s -> Mutator t
- type SimpleSetting s a = Setting s s a a
- type SimpleSetter s a = Setter s s a a
- type SimpleReifiedSetter s a = ReifiedSetter s s a a
- class Applicative f => Settable f
- data Mutator a
Setters
type Setter s t a b = forall f. Settable f => (a -> f b) -> s -> f tSource
The only Lens-like law that can apply to a Setter l is that
setl y (setl x a) ≡setl y 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.
>>>over traverse f [a,b,c,d][f a,f b,f c,f d]
>>>over _1 f (a,b)(f a,b)
>>>over (traverse._1) f [(a,b),(c,d)][(f a,b),(f c,d)]
>>>over both f (a,b)(f a,f b)
>>>over (traverse.both) f [(a,b),(c,d)][(f a,f b),(f c,f d)]
Building Setters
Common Setters
mapped :: Functor f => Setter (f a) (f b) a bSource
This setter can be used to map over all of the values in a Functor.
fmap≡overmappedfmapDefault≡overtraverse(<$) ≡setmapped
>>>over mapped f [a,b,c][f a,f b,f c]
>>>over mapped (+1) [1,2,3][2,3,4]
>>>set mapped x [a,b,c][x,x,x]
>>>[[a,b],[c]] & mapped.mapped +~ x[[a + x,b + x],[c + x]]
>>>over (mapped._2) length [("hello","world"),("leaders","!!!")][("hello",5),("leaders",3)]
Functional Combinators
over :: Setting s t a b -> (a -> b) -> s -> tSource
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
Given any valid Setter l, you can also rely on the law:
overl f .overl g =overl (f . g)
e.g.
>>>over mapped f (over mapped g [a,b,c]) == over mapped (f . g) [a,b,c]True
Another way to view over is to say that it transforms a Setter into a
"semantic editor combinator".
>>>over mapped f (Just a)Just (f a)
>>>over mapped (*10) [1,2,3][10,20,30]
>>>over _1 f (a,b)(f a,b)
>>>over _1 show (10,20)("10",20)
over::Setters t a b -> (a -> b) -> s -> t
set :: Setting s t a b -> b -> s -> tSource
Replace the target of a Lens or all of the targets of a Setter
or Traversal with a constant value.
(<$) ≡setmapped
>>>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::Setters t a b -> b -> s -> tset::Isos t a b -> b -> s -> tset::Lenss t a b -> b -> s -> tset::Traversals t a b -> b -> s -> t
(.~) :: Setting s t a b -> b -> s -> tSource
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
>>>(a,b,c,d) & _4 .~ e(a,b,c,e)
>>>(42,"world") & _1 .~ "hello"("hello","world")
>>>(a,b) & both .~ c(c,c)
(.~) ::Setters t a b -> b -> s -> t (.~) ::Isos t a b -> b -> s -> t (.~) ::Lenss t a b -> b -> s -> t (.~) ::Traversals t a b -> b -> s -> t
(%~) :: Setting s t a b -> (a -> b) -> s -> tSource
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
>>>(a,b,c) & _3 %~ f(a,b,f c)
>>>(a,b) & both %~ f(f a,f b)
>>>_2 %~ length $ (1,"hello")(1,5)
>>>traverse %~ f $ [a,b,c][f a,f b,f c]
>>>traverse %~ even $ [1,2,3][False,True,False]
>>>traverse.traverse %~ length $ [["hello","world"],["!!!"]][[5,5],[3]]
(%~) ::Setters t a b -> (a -> b) -> s -> t (%~) ::Isos t a b -> (a -> b) -> s -> t (%~) ::Lenss t a b -> (a -> b) -> s -> t (%~) ::Traversals t a b -> (a -> b) -> s -> t
(+~) :: Num a => Setting s t a a -> a -> s -> tSource
Increment the target(s) of a numerically valued Lens, Setter or Traversal
>>>(a,b) & _1 +~ c(a + c,b)
>>>(a,b) & both +~ c(a + c,b + c)
>>>(1,2) & _2 +~ 1(1,3)
>>>[(a,b),(c,d)] & traverse.both +~ e[(a + e,b + e),(c + e,d + e)]
(+~) :: Num a =>SimpleSetters a -> a -> s -> s (+~) :: Num a =>SimpleIsos a -> a -> s -> s (+~) :: Num a =>SimpleLenss a -> a -> s -> s (+~) :: Num a =>SimpleTraversals a -> a -> s -> s
(-~) :: Num a => Setting s t a a -> a -> s -> tSource
Decrement the target(s) of a numerically valued Lens, Iso, Setter or Traversal
>>>(a,b) & _1 -~ c(a - c,b)
>>>(a,b) & both -~ c(a - c,b - c)
>>>_1 -~ 2 $ (1,2)(-1,2)
>>>mapped.mapped -~ 1 $ [[4,5],[6,7]][[3,4],[5,6]]
(-~) ::Numa =>SimpleSetters a -> a -> s -> s (-~) ::Numa =>SimpleIsos a -> a -> s -> s (-~) ::Numa =>SimpleLenss a -> a -> s -> s (-~) ::Numa =>SimpleTraversals a -> a -> s -> s
(*~) :: Num a => Setting s t a a -> a -> s -> tSource
Multiply the target(s) of a numerically valued Lens, Iso, Setter or Traversal
>>>(a,b) & _1 *~ c(a * c,b)
>>>(a,b) & both *~ c(a * c,b * c)
>>>(1,2) & _2 *~ 4(1,8)
>>>Just 24 & mapped *~ 2Just 48
(*~) ::Numa =>SimpleSetters a -> a -> s -> s (*~) ::Numa =>SimpleIsos a -> a -> s -> s (*~) ::Numa =>SimpleLenss a -> a -> s -> s (*~) ::Numa =>SimpleTraversals a -> a -> s -> s
(//~) :: Fractional s => Setting a b s s -> s -> a -> bSource
Divide the target(s) of a numerically valued Lens, Iso, Setter or Traversal
>>>(a,b) & _1 //~ c(a / c,b)
>>>(a,b) & both //~ c(a / c,b / c)
>>>("Hawaii",10) & _2 //~ 2("Hawaii",5.0)
(//~) ::Fractionala =>SimpleSetters a -> a -> s -> s (//~) ::Fractionala =>SimpleIsos a -> a -> s -> s (//~) ::Fractionala =>SimpleLenss a -> a -> s -> s (//~) ::Fractionala =>SimpleTraversals a -> a -> s -> s
(^~) :: (Num a, Integral e) => Setting s t a a -> e -> s -> tSource
Raise the target(s) of a numerically valued Lens, Setter or Traversal to a non-negative integral power
>>>(1,3) & _2 ^~ 2(1,9)
(^~) :: (Numa,Integrale) =>SimpleSetters a -> e -> s -> s (^~) :: (Numa,Integrale) =>SimpleIsos a -> e -> s -> s (^~) :: (Numa,Integrale) =>SimpleLenss a -> e -> s -> s (^~) :: (Numa,Integrale) =>SimpleTraversals a -> e -> s -> s
(^^~) :: (Fractional a, Integral e) => Setting s t a a -> e -> s -> tSource
Raise the target(s) of a fractionally valued Lens, Setter or Traversal to an integral power
>>>(1,2) & _2 ^^~ (-1)(1,0.5)
(^^~) :: (Fractionala,Integrale) =>SimpleSetters a -> e -> s -> s (^^~) :: (Fractionala,Integrale) =>SimpleIsos a -> e -> s -> s (^^~) :: (Fractionala,Integrale) =>SimpleLenss a -> e -> s -> s (^^~) :: (Fractionala,Integrale) =>SimpleTraversals a -> e -> s -> s
(**~) :: Floating a => Setting s t a a -> a -> s -> tSource
Raise the target(s) of a floating-point valued Lens, Setter or Traversal to an arbitrary power.
>>>(a,b) & _1 **~ c(a**c,b)
>>>(a,b) & both **~ c(a**c,b**c)
>>>_2 **~ pi $ (1,3)(1,31.54428070019754)
(**~) ::Floatinga =>SimpleSetters a -> a -> s -> s (**~) ::Floatinga =>SimpleIsos a -> a -> s -> s (**~) ::Floatinga =>SimpleLenss a -> a -> s -> s (**~) ::Floatinga =>SimpleTraversals a -> a -> s -> s
(||~) :: Setting s t Bool Bool -> Bool -> s -> tSource
Logically || the target(s) of a Bool-valued Lens or Setter
>>>both ||~ True $ (False,True)(True,True)
>>>both ||~ False $ (False,True)(False,True)
(||~) ::SimpleSettersBool->Bool-> s -> s (||~) ::SimpleIsosBool->Bool-> s -> s (||~) ::SimpleLenssBool->Bool-> s -> s (||~) ::SimpleTraversalsBool->Bool-> s -> s
(<>~) :: Monoid a => Setting s t a a -> a -> s -> tSource
Modify the target of a monoidally valued by mappending another value.
>>>(Sum a,b) & _1 <>~ Sum c(Sum {getSum = a + c},b)
>>>(Sum a,Sum b) & both <>~ Sum c(Sum {getSum = a + c},Sum {getSum = b + c})
>>>both <>~ "!!!" $ ("hello","world")("hello!!!","world!!!")
(<>~) ::Monoida =>Setters t a a -> a -> s -> t (<>~) ::Monoida =>Isos t a a -> a -> s -> t (<>~) ::Monoida =>Lenss t a a -> a -> s -> t (<>~) ::Monoida =>Traversals t a a -> a -> s -> t
(&&~) :: Setting s t Bool Bool -> Bool -> s -> tSource
Logically && the target(s) of a Bool-valued Lens or Setter
>>>both &&~ True $ (False, True)(False,True)
>>>both &&~ False $ (False, True)(False,False)
(&&~) ::SimpleSettersBool->Bool-> s -> s (&&~) ::SimpleIsosBool->Bool-> s -> s (&&~) ::SimpleLenssBool->Bool-> s -> s (&&~) ::SimpleTraversalsBool->Bool-> s -> s
(<.~) :: Setting s t a b -> b -> s -> (b, t)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.
.~ t
>>>(a,b) & _1 <.~ c(c,(c,b))
>>>("good","morning","vietnam") & _3 <.~ "world"("world",("good","morning","world"))
>>>(42,Map.fromList [("goodnight","gracie")]) & _2.at "hello" <.~ Just "world"(Just "world",(42,fromList [("goodnight","gracie"),("hello","world")]))
(<.~) ::Setters t a b -> b -> s -> (b, t) (<.~) ::Isos t a b -> b -> s -> (b, t) (<.~) ::Lenss t a b -> b -> s -> (b, t) (<.~) ::Traversals t a b -> b -> s -> (b, t)
(?~) :: Setting s t a (Maybe b) -> b -> s -> tSource
Set the target of a Lens, Traversal or Setter to Just a value.
l?~t ≡setl (Justt)
>>>Nothing & id ?~ aJust a
>>>Map.empty & at 3 ?~ xfromList [(3,x)]
(?~) ::Setters t a (Maybeb) -> b -> s -> t (?~) ::Isos t a (Maybeb) -> b -> s -> t (?~) ::Lenss t a (Maybeb) -> b -> s -> t (?~) ::Traversals t a (Maybeb) -> b -> s -> t
(<?~) :: Setting s t a (Maybe b) -> b -> s -> (b, t)Source
Set to Just a value 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
>>>import Data.Map as Map>>>_2.at "hello" <?~ "world" $ (42,Map.fromList [("goodnight","gracie")])("world",(42,fromList [("goodnight","gracie"),("hello","world")]))
(<?~) ::Setters t a b -> (Maybeb) -> s -> (b, t) (<?~) ::Isos t a (Maybeb) -> b -> s -> (b, t) (<?~) ::Lenss t a (Maybeb) -> b -> s -> (b, t) (<?~) ::Traversals t a (Maybeb) -> b -> s -> (b, t)
State Combinators
assign :: MonadState s m => Setting s s a b -> b -> 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.
This is an alias for (.=).
>>>execState (do assign _1 c; assign _2 d) (a,b)(c,d)
>>>execState (both .= c) (a,b)(c,c)
assign::MonadStates m =>SimpleIsos a -> a -> m ()assign::MonadStates m =>SimpleLenss a -> a -> m ()assign::MonadStates m =>SimpleTraversals a -> a -> m ()assign::MonadStates m =>SimpleSetters a -> a -> m ()
(.=) :: MonadState s m => Setting s s a b -> b -> 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.
This is an infix version of assign.
>>>execState (do _1 .= c; _2 .= d) (a,b)(c,d)
>>>execState (both .= c) (a,b)(c,c)
(.=) ::MonadStates m =>SimpleIsos a -> a -> m () (.=) ::MonadStates m =>SimpleLenss a -> a -> m () (.=) ::MonadStates m =>SimpleTraversals a -> a -> m () (.=) ::MonadStates m =>SimpleSetters a -> a -> m ()
(%=) :: MonadState s m => Setting s s a b -> (a -> b) -> m ()Source
Map over the target of a Lens or all of the targets of a Setter or Traversal in our monadic state.
>>>execState (do _1 %= f;_2 %= g) (a,b)(f a,g b)
>>>execState (do both %= f) (a,b)(f a,f b)
(%=) ::MonadStates m =>SimpleIsos a -> (a -> a) -> m () (%=) ::MonadStates m =>SimpleLenss a -> (a -> a) -> m () (%=) ::MonadStates m =>SimpleTraversals a -> (a -> a) -> m () (%=) ::MonadStates m =>SimpleSetters a -> (a -> a) -> m ()
(+=) :: (MonadState s m, Num a) => SimpleSetting s a -> a -> 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
>>>execState (do _1 += c; _2 += d) (a,b)(a + c,b + d)
>>>execState (do _1.at 1.non 0 += 10) (Map.fromList [(2,100)],"hello")(fromList [(1,10),(2,100)],"hello")
(+=) :: (MonadStates m,Numa) =>SimpleSetters a -> a -> m () (+=) :: (MonadStates m,Numa) =>SimpleIsos a -> a -> m () (+=) :: (MonadStates m,Numa) =>SimpleLenss a -> a -> m () (+=) :: (MonadStates m,Numa) =>SimpleTraversals a -> a -> m ()
(-=) :: (MonadState s m, Num a) => SimpleSetting s a -> a -> m ()Source
Modify the target(s) of a Simple Lens, Iso, Setter or Traversal by subtracting a value
>>>execState (do _1 -= c; _2 -= d) (a,b)(a - c,b - d)
(-=) :: (MonadStates m,Numa) =>SimpleSetters a -> a -> m () (-=) :: (MonadStates m,Numa) =>SimpleIsos a -> a -> m () (-=) :: (MonadStates m,Numa) =>SimpleLenss a -> a -> m () (-=) :: (MonadStates m,Numa) =>SimpleTraversals a -> a -> m ()
(*=) :: (MonadState s m, Num a) => SimpleSetting s a -> a -> m ()Source
Modify the target(s) of a Simple Lens, Iso, Setter or Traversal by multiplying by value.
>>>execState (do _1 *= c; _2 *= d) (a,b)(a * c,b * d)
(*=) :: (MonadStates m,Numa) =>SimpleSetters a -> a -> m () (*=) :: (MonadStates m,Numa) =>SimpleIsos a -> a -> m () (*=) :: (MonadStates m,Numa) =>SimpleLenss a -> a -> m () (*=) :: (MonadStates m,Numa) =>SimpleTraversals a -> a -> m ()
(//=) :: (MonadState s m, Fractional a) => SimpleSetting s a -> a -> m ()Source
Modify the target(s) of a Simple Lens, Iso, Setter or Traversal by dividing by a value.
>>>execState (do _1 //= c; _2 //= d) (a,b)(a / c,b / d)
(//=) :: (MonadStates m,Fractionala) =>SimpleSetters a -> a -> m () (//=) :: (MonadStates m,Fractionala) =>SimpleIsos a -> a -> m () (//=) :: (MonadStates m,Fractionala) =>SimpleLenss a -> a -> m () (//=) :: (MonadStates m,Fractionala) =>SimpleTraversals a -> a -> m ()
(^=) :: (MonadState s m, Num a, Integral e) => SimpleSetting s a -> e -> m ()Source
Raise the target(s) of a numerically valued Lens, Setter or Traversal to a non-negative integral power.
(^=) :: (MonadStates m,Numa,Integrale) =>SimpleSetters a -> e -> m () (^=) :: (MonadStates m,Numa,Integrale) =>SimpleIsos a -> e -> m () (^=) :: (MonadStates m,Numa,Integrale) =>SimpleLenss a -> e -> m () (^=) :: (MonadStates m,Numa,Integrale) =>SimpleTraversals a -> e -> m ()
(^^=) :: (MonadState s m, Fractional a, Integral e) => SimpleSetting s a -> e -> m ()Source
Raise the target(s) of a numerically valued Lens, Setter or Traversal to an integral power.
(^^=) :: (MonadStates m,Fractionala,Integrale) =>SimpleSetters a -> e -> m () (^^=) :: (MonadStates m,Fractionala,Integrale) =>SimpleIsos a -> e -> m () (^^=) :: (MonadStates m,Fractionala,Integrale) =>SimpleLenss a -> e -> m () (^^=) :: (MonadStates m,Fractionala,Integrale) =>SimpleTraversals a -> e -> m ()
(**=) :: (MonadState s m, Floating a) => SimpleSetting s a -> a -> m ()Source
Raise the target(s) of a numerically valued Lens, Setter or Traversal to an arbitrary power
>>>execState (do _1 **= c; _2 **= d) (a,b)(a**c,b**d)
(**=) :: (MonadStates m,Floatinga) =>SimpleSetters a -> a -> m () (**=) :: (MonadStates m,Floatinga) =>SimpleIsos a -> a -> m () (**=) :: (MonadStates m,Floatinga) =>SimpleLenss a -> a -> m () (**=) :: (MonadStates m,Floatinga) =>SimpleTraversals a -> a -> m ()
(||=) :: MonadState s m => SimpleSetting s Bool -> Bool -> m ()Source
Modify the target(s) of a Simple Lens, 'Iso, Setter or Traversal by taking their logical || with a value
>>>execState (do _1 ||= True; _2 ||= False; _3 ||= True; _4 ||= False) (True,True,False,False)(True,True,True,False)
(||=) ::MonadStates m =>SimpleSettersBool->Bool-> m () (||=) ::MonadStates m =>SimpleIsosBool->Bool-> m () (||=) ::MonadStates m =>SimpleLenssBool->Bool-> m () (||=) ::MonadStates m =>SimpleTraversalsBool->Bool-> m ()
(<>=) :: (MonadState s m, Monoid a) => SimpleSetting s a -> a -> m ()Source
Modify the target(s) of a Simple Lens, Iso, Setter or Traversal by mappending a value.
>>>execState (do _1 <>= Sum c; _2 <>= Product d) (Sum a,Product b)(Sum {getSum = a + c},Product {getProduct = b * d})
>>>execState (both <>= "!!!") ("hello","world")("hello!!!","world!!!")
(<>=) :: (MonadStates m,Monoida) =>SimpleSetters a -> a -> m () (<>=) :: (MonadStates m,Monoida) =>SimpleIsos a -> a -> m () (<>=) :: (MonadStates m,Monoida) =>SimpleLenss a -> a -> m () (<>=) :: (MonadStates m,Monoida) =>SimpleTraversals a -> a -> m ()
(&&=) :: MonadState s m => SimpleSetting s Bool -> Bool -> m ()Source
Modify the target(s) of a Simple Lens, Iso, Setter or Traversal by taking their logical && with a value
>>>execState (do _1 &&= True; _2 &&= False; _3 &&= True; _4 &&= False) (True,True,False,False)(True,False,False,False)
(&&=) ::MonadStates m =>SimpleSettersBool->Bool-> m () (&&=) ::MonadStates m =>SimpleIsosBool->Bool-> m () (&&=) ::MonadStates m =>SimpleLenssBool->Bool-> m () (&&=) ::MonadStates m =>SimpleTraversalsBool->Bool-> m ()
(<.=) :: MonadState s m => Setting s s a b -> b -> m bSource
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
(<.=) ::MonadStates m =>Setters s a b -> b -> m b (<.=) ::MonadStates m =>Isos s a b -> b -> m b (<.=) ::MonadStates m =>Lenss s a b -> b -> m b (<.=) ::MonadStates m =>Traversals s a b -> b -> m b
(?=) :: MonadState s m => Setting s s a (Maybe b) -> b -> m ()Source
Replace the target of a Lens or all of the targets of a Setter or Traversal in our monadic
state with Just a new value, irrespective of the old.
>>>execState (do at 1 ?= a; at 2 ?= b) Map.emptyfromList [(1,a),(2,b)]
>>>execState (do _1 ?= b; _2 ?= c) (Just a, Nothing)(Just b,Just c)
(?=) ::MonadStates m =>SimpleIsos (Maybea) -> a -> m () (?=) ::MonadStates m =>SimpleLenss (Maybea) -> a -> m () (?=) ::MonadStates m =>SimpleTraversals (Maybea) -> a -> m () (?=) ::MonadStates m =>SimpleSetters (Maybea) -> a -> m ()
(<?=) :: MonadState s m => Setting s s a (Maybe b) -> b -> m bSource
Set Just a value with pass-through
This is useful for chaining assignment without round-tripping through your monad stack.
do x <- at foo <?= ninety_nine_bottles_of_beer_on_the_wall
If you do not need a copy of the intermediate result, then using l ?= d will avoid unused binding warnings
(<?=) ::MonadStates m =>Setters s a (Maybeb) -> b -> m b (<?=) ::MonadStates m =>Isos s a (Maybeb) -> b -> m b (<?=) ::MonadStates m =>Lenss s a (Maybeb) -> b -> m b (<?=) ::MonadStates m =>Traversals s a (Maybeb) -> b -> m b
(<~) :: MonadState s m => Setting s s a b -> m b -> m ()Source
Run a monadic action, and set all of the targets of a Lens, Setter or Traversal to its result.
(<~) ::MonadStates m =>Isos s a b -> m b -> m () (<~) ::MonadStates m =>Lenss s a b -> m b -> m () (<~) ::MonadStates m =>Traversals s a b -> m b -> m () (<~) ::MonadStates m =>Setters s a b -> m b -> 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
...
Simplified State Setting
set' :: Setting s s a a -> a -> s -> sSource
Replace the target of a Lens or all of the targets of a Simple Setter
or Simple Traversal with a constant value, without changing its type.
This is a type restricted version of set, which retains the type of the original.
>>>set' mapped x [a,b,c,d][x,x,x,x]
>>>set' _2 "hello" (1,"world")(1,"hello")
>>>set' mapped 0 [1,2,3,4][0,0,0,0]
Note: Attempting to adjust set' a Fold or Getter will fail at compile time with an
relatively nice error message.
set'::SimpleSetters a -> a -> s -> sset'::SimpleIsos a -> a -> s -> sset'::SimpleLenss a -> a -> s -> sset'::SimpleTraversals a -> a -> s -> s
Storing Setters
newtype ReifiedSetter s t a b Source
Reify a setter so it can be stored safely in a container.
Constructors
| ReifySetter | |
Fields
| |
Setter Internals
type SimpleSetting s a = Setting s s a aSource
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 s a = Setter s s a aSource
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
type SimpleReifiedSetter s a = ReifiedSetter s s a aSource
Exported for legible error messages
class Applicative f => Settable f Source