22
33module Data.Lens.Setter
44 ( (%~), (.~), (+~), (-~), (*~), (//~), (||~), (&&~), (<>~), (++~), (?~)
5+ , (.=), (%=), (+=), (*=), (-=), (//=), (||=), (&&=), (<>=), (++=), (?=)
56 , over , set
67 , module Data.Lens.Types
78 ) where
89
910import Prelude
1011
12+ import Control.Monad.State.Class (MonadState , modify )
1113import Data.Maybe (Maybe (..))
1214
1315import Data.Lens.Types (Setter (), SetterP ())
@@ -24,6 +26,18 @@ infixr 4 <>~
2426infixr 4 ++~
2527infixr 4 ?~
2628
29+ infix 4 .=
30+ infix 4 %=
31+ infix 4 +=
32+ infix 4 *=
33+ infix 4 -=
34+ infix 4 //=
35+ infix 4 ||=
36+ infix 4 &&=
37+ infix 4 <>=
38+ infix 4 ++=
39+ infix 4 ?=
40+
2741-- | Apply a function to the foci of a `Setter`.
2842over :: forall s t a b . Setter s t a b -> (a -> b ) -> s -> t
2943over l = l
@@ -66,3 +80,48 @@ set l b = over l (const b)
6680
6781(?~) :: forall s t a b . Setter s t a (Maybe b ) -> b -> s -> t
6882(?~) p = set p <<< Just
83+
84+ -- Stateful
85+
86+ -- | Set the foci of a `Setter` in a monadic state to a constant value.
87+ assign :: forall s a b m . (MonadState s m ) => Setter s s a b -> b -> m Unit
88+ assign p b = modify (set p b)
89+
90+ -- | Modify the foci of a `Setter` in a monadic state.
91+ modifying :: forall s a b m . (MonadState s m ) => Setter s s a b -> (a -> b ) -> m Unit
92+ modifying p f = modify (over p f)
93+
94+ -- | Synonym for `assign`
95+ (.=) :: forall s a b m . (MonadState s m ) => Setter s s a b -> b -> m Unit
96+ (.=) = assign
97+
98+ -- | Synonym for `modifying`
99+ (%=) :: forall s a b m . (MonadState s m ) => Setter s s a b -> (a -> b ) -> m Unit
100+ (%=) = modifying
101+
102+ (+=) :: forall s a m . (MonadState s m , Semiring a ) => SetterP s a -> a -> m Unit
103+ (+=) p = modifying p <<< add
104+
105+ (*=) :: forall s a m . (MonadState s m , Semiring a ) => SetterP s a -> a -> m Unit
106+ (*=) p = modifying p <<< flip mul
107+
108+ (-=) :: forall s a m . (MonadState s m , Ring a ) => SetterP s a -> a -> m Unit
109+ (-=) p = modifying p <<< flip sub
110+
111+ (//=) :: forall s a m . (MonadState s m , DivisionRing a ) => SetterP s a -> a -> m Unit
112+ (//=) p = modifying p <<< flip div
113+
114+ (||=) :: forall s a m . (MonadState s m , BooleanAlgebra a ) => SetterP s a -> a -> m Unit
115+ (||=) p = modifying p <<< flip disj
116+
117+ (&&=) :: forall s a m . (MonadState s m , BooleanAlgebra a ) => SetterP s a -> a -> m Unit
118+ (&&=) p = modifying p <<< flip conj
119+
120+ (<>=) :: forall s a m . (MonadState s m , Semigroup a ) => SetterP s a -> a -> m Unit
121+ (<>=) p = modifying p <<< flip append
122+
123+ (++=) :: forall s a m . (MonadState s m , Semigroup a ) => SetterP s a -> a -> m Unit
124+ (++=) p = modifying p <<< flip append
125+
126+ (?=) :: forall s a b m . (MonadState s m ) => Setter s s a (Maybe b ) -> b -> m Unit
127+ (?=) p = assign p <<< Just
0 commit comments