Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added `modifyAndGet` helper function.

Thanks to Nikita Volkov.
  • Loading branch information...
commit e9645f4c51afb67cfc9f22d0a040b56752e32949 1 parent aefc0d4
@sebastiaanvisser authored
Showing with 20 additions and 4 deletions.
  1. +4 −0 CHANGELOG
  2. +4 −4 fclabels.cabal
  3. +12 −0 src/Data/Label/PureM.hs
View
4 CHANGELOG
@@ -1,5 +1,9 @@
CHANGELOG
+1.1.4.3 -> 1.1.5
+ - Added `modifyAndGet` helper function.
+ Thanks to Nikita Volkov.
+
1.1.4.2 -> 1.1.4.3
- Make compilable against Template Haskell 2.8.
View
8 fclabels.cabal
@@ -1,5 +1,5 @@
Name: fclabels
-Version: 1.1.4.3
+Version: 1.1.5
Author: Sebastiaan Visser, Erik Hesselink, Chris Eidhof, Sjoerd Visscher
with lots of help and feedback from others.
Synopsis: First class accessor labels.
@@ -20,9 +20,9 @@ Description: This package provides first class labels that can act as
.
See the "Data.Label.Maybe" module for the use of partial labels.
.
- > 1.1.4.2 -> 1.1.4.3
- > - Make compilable against template haskell 2.8.
- > Thanks to mgsloan for the pull request.
+ > 1.1.4.3 -> 1.1.5
+ > - Added `modifyAndGet` helper function.
+ > Thanks to Nikita Volkov.
Maintainer: Sebastiaan Visser <code@fvisser.nl>
License: BSD3
View
12 src/Data/Label/PureM.hs
@@ -5,6 +5,7 @@ module Data.Label.PureM
gets
, puts
, modify
+, modifyAndGet
, (=:)
, (=.)
@@ -14,6 +15,7 @@ module Data.Label.PureM
)
where
+import Control.Monad
import Data.Label.Pure ((:->))
import qualified Control.Monad.Reader as M
import qualified Control.Monad.State as M
@@ -58,3 +60,13 @@ asks = M.asks . L.get
local :: M.MonadReader r m => (r :-> b) -> (b -> b) -> m a -> m a
local l f = M.local (L.modify l f)
+-- | Modify a value with a function somewhere in the state, pointed to by the
+-- specified lens. Additionally return a separate value based on the
+-- modification.
+
+modifyAndGet :: M.MonadState s m => (s :-> a) -> (a -> (b, a)) -> m b
+modifyAndGet l f =
+ do (b, a) <- f `liftM` gets l
+ puts l a
+ return b
+
Please sign in to comment.
Something went wrong with that request. Please try again.