monad-statevar-0.1: Concise, overloaded accessors for IORef, STRef, TVar

Safe HaskellTrustworthy

Control.Monad.StateVar

Contents

Description

Overloaded get and put for state variables (IORef, TVar, etc.) to make reading and writing more concise.

Synopsis

Overloaded get and put

class HasGet m v whereSource

Methods

get :: v a -> m aSource

Read the value from the variable.

class HasPut m v whereSource

Methods

put :: v a -> a -> m ()Source

Write a new value to the variable.

put' :: HasPut m v => v a -> a -> m ()Source

Variant of put that forces the value before writing it.

modify :: (HasGet m v, HasPut m v, Monad m) => v a -> (a -> a) -> m ()Source

Modify the value inside the variable with the given function.

list <- newIORef [1,2,3]
modify list (0:)  -- prepend 0 to the list

modify' :: (HasGet m v, HasPut m v, Monad m) => v a -> (a -> a) -> m ()Source

Variant of modify that forces the result of the function.

tally <- newIORef (0 :: Int)
modify tally (+ 10)  -- add 10 to the tally

swap :: (HasGet m v, HasPut m v, Monad m) => v a -> a -> m aSource

Write a new value and return the old value.

Infix aliases

($=) :: HasPut m v => v a -> a -> m ()Source

Infix alias for put

($~) :: (HasGet m v, HasPut m v, Monad m) => v a -> (a -> a) -> m ()Source

Infix alias for modify

($=!) :: HasPut m v => v a -> a -> m ()Source

Infix alias for put'

($~!) :: (HasGet m v, HasPut m v, Monad m) => v a -> (a -> a) -> m ()Source

Infix alias for modify'