File: State.hs

package info (click to toggle)
github-backup 1.20140831
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 356 kB
  • ctags: 4
  • sloc: haskell: 3,384; makefile: 29; sh: 10
file content (28 lines) | stat: -rw-r--r-- 698 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{- state monad support
 -
 - Copyright 2012 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

{-# LANGUAGE PackageImports #-}

module Utility.State where

import "mtl" Control.Monad.State.Strict

{- Modifies Control.Monad.State's state, forcing a strict update.
 - This avoids building thunks in the state and leaking.
 - Why it's not the default, I don't know.
 -
 - Example: changeState $ \s -> s { foo = bar }
 -}
changeState :: MonadState s m => (s -> s) -> m ()
changeState f = do
	x <- get
	put $! f x

{- Gets a value from the internal state, selected by the passed value
 - constructor. -}
getState :: MonadState s m => (s -> a) -> m a
getState = gets