Permalink
Browse files

Adding Data.Lens.Lazy.Void module which duplicates Data.Lens.Lazy but…

… returns m () for setting operations.

This makes the setting operations easier to use with `when` and `unless`.
  • Loading branch information...
roconnor committed Jun 10, 2012
1 parent 5d26442 commit 4195f6e7080892d12678c34e1eab6e7558e62814
Showing with 62 additions and 0 deletions.
  1. +1 −0 data-lens.cabal
  2. +61 −0 src/Data/Lens/Lazy/Void.hs
View
@@ -41,6 +41,7 @@ library
exposed-modules:
Data.Lens.Common
Data.Lens.Lazy
+ Data.Lens.Lazy.Void
Data.Lens.Strict
Data.Lens.Partial.Common
Data.Lens.Partial.Lazy
View
@@ -0,0 +1,61 @@
+module Data.Lens.Lazy.Void
+ ( module Data.Lens.Common
+ -- * State API
+ , access -- getter -- :: Monad m => Lens a b -> StateT a m b
+ , (~=), (!=) -- setter -- :: Monad m => Lens a b -> b -> StateT a m b
+ , (%=), (!%=) -- modify -- :: Monad m => Lens a b -> (b -> b) -> StateT a m b
+ , (%%=), (!%%=) -- modify -- :: Monad m => Lens a b -> (b -> (c, b)) -> StateT a m c
+ , (+=), (!+=) -- modify -- :: (Monad m, Num b) => Lens a b -> b -> StateT a m b
+ , (-=), (!-=) -- modify -- :: (Monad m, Num b) => Lens a b -> b -> StateT a m b
+ , (*=), (!*=) -- modify -- :: (Monad m, Num b) => Lens a b -> b -> StateT a m b
+ , (//=), (!/=) -- modify -- :: (Monad m, Fractional b) => Lens a b -> b -> StateT a m b
+ , (&&=), (!&&=) -- modify -- :: Monad m => Lens a Bool -> Bool -> StateT a m Bool
+ , (||=), (!||=) -- modify -- :: Monad m => Lens a Bool -> Bool -> StateT a m Bool
+ , focus -- modify -- :: Monad m => Lens a b -> StateT m b c -> StateT m a c
+ ) where
+
+import Data.Lens.Common
+import Control.Monad (void)
+import Control.Monad.Trans.State
+import Data.Lens.Lazy (access, (%%=), (!%%=), focus)
+import qualified Data.Lens.Lazy as L
+
+-- * State actions
+
+infixr 4 ~=, !=
+
+-- | set a value using a lens into state
+(~=), (!=) :: (Functor m, Monad m) => Lens a b -> b -> StateT a m ()
+l ~= b = void (l L.~= b)
+l != b = void (l L.!= b)
+
+infixr 4 %=, !%=
+
+-- | infix modification a value through a lens into state
+(%=), (!%=) :: (Functor m, Monad m) => Lens a b -> (b -> b) -> StateT a m ()
+l %= f = void (l L.%= f)
+l !%= f = void (l L.!%= f)
+
+infixr 4 +=, !+=, -=, !-=, *=, !*=
+
+(+=), (!+=), (-=), (!-=), (*=), (!*=) :: (Functor m, Monad m, Num b) => Lens a b -> b -> StateT a m ()
+f += b = f %= (+ b)
+f -= b = f %= subtract b
+f *= b = f %= (* b)
+f !+= b = f !%= (+ b)
+f !-= b = f !%= subtract b
+f !*= b = f !%= (* b)
+
+infixr 4 //=, !/=
+
+(//=), (!/=) :: (Functor m, Monad m, Fractional b) => Lens a b -> b -> StateT a m ()
+f //= b = f %= (/ b)
+f !/= b = f !%= (/ b)
+
+infixr 4 &&=, !&&=, ||=, !||=
+
+(&&=), (||=), (!&&=), (!||=) :: (Functor m, Monad m) => Lens a Bool -> Bool -> StateT a m ()
+f &&= b = f %= (&& b)
+f ||= b = f %= (|| b)
+f !&&= b = f !%= (&& b)
+f !||= b = f !%= (|| b)

0 comments on commit 4195f6e

Please sign in to comment.