2012-05-12 04:27:59 +00:00
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
2013-04-08 00:55:34 +00:00
|
|
|
module Guid (guid, set, run, runAt, GuidCounter) where
|
2012-05-12 04:27:59 +00:00
|
|
|
|
|
|
|
import Control.Monad.State (evalState, State, get, put)
|
|
|
|
|
|
|
|
-- Wrapper around State monad.
|
|
|
|
newtype GuidCounter a = GC { runGC :: State Int a }
|
|
|
|
deriving (Monad)
|
|
|
|
|
|
|
|
-- Get the next GUID, incrementing the counter.
|
|
|
|
guid :: GuidCounter Int
|
|
|
|
guid = GC $ do n <- get
|
|
|
|
put (n + 1)
|
|
|
|
return n
|
|
|
|
|
2013-04-08 00:55:34 +00:00
|
|
|
set n = GC (put n)
|
|
|
|
|
2013-02-09 21:02:24 +00:00
|
|
|
run = runAt 0
|
|
|
|
runAt n x = evalState (runGC x) n
|