Skip to content

Commit

Permalink
Adding Data.Lens.Lazy.Void module which duplicates Data.Lens.Lazy but…
Browse files Browse the repository at this point in the history
… 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 4195f6e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions data-lens.cabal
Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions src/Data/Lens/Lazy/Void.hs
@@ -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.