Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. {-# LANGUAGE RecordWildCards #-}
  2.  
  3. import Data.IORef
  4. import System.Clock
  5.  
  6.  
  7. data Timer =
  8. Timer {
  9. _timerRef :: !(IORef TimeSpec),
  10. _timerType :: !Clock
  11. }
  12.  
  13.  
  14. newTimer :: Clock -> IO Timer
  15. newTimer _timerType =
  16. fmap (\_timerRef -> Timer{..})
  17. (getTime _timerType >>= newIORef)
  18.  
  19.  
  20. timed :: Timer -> IO a -> IO a
  21. timed Timer{..} action = do
  22. t0 <- getTime _timerType
  23. r <- action
  24. t1 <- getTime _timerType
  25. let dt = t1 - t0
  26. r <$ atomicModifyIORef' _timerRef (\t -> (t + dt, ()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement