diff options
author | Jesse Rosenthal <jrosenthal@jhu.edu> | 2016-11-19 05:31:32 -0500 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2017-01-25 17:07:39 +0100 |
commit | 8b144db6e575e56205de881d8ae233fe2ff828da (patch) | |
tree | 545419d319dbcd2e478993405516d52df73d1007 | |
parent | 1c589c51b13aa6833cf6246b514ce8ddadf25dd5 (diff) | |
download | pandoc-8b144db6e575e56205de881d8ae233fe2ff828da.tar.gz |
Write Pure uuid function taking stdgen.
We're trying to cut down the necessarily IO functions. Since we alerady
have a newStdGen function, we don't need this one.
-rw-r--r-- | src/Text/Pandoc/UUID.hs | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/src/Text/Pandoc/UUID.hs b/src/Text/Pandoc/UUID.hs index 6d6e22944..9d8cd4434 100644 --- a/src/Text/Pandoc/UUID.hs +++ b/src/Text/Pandoc/UUID.hs @@ -29,13 +29,12 @@ UUID generation using Version 4 (random method) described in RFC4122. See http://tools.ietf.org/html/rfc4122 -} -module Text.Pandoc.UUID ( UUID(..), getRandomUUID ) where +module Text.Pandoc.UUID ( UUID(..), getRandomUUID, getUUID ) where import Text.Printf ( printf ) -import System.Random ( randomIO ) +import System.Random ( RandomGen, randoms, getStdGen ) import Data.Word import Data.Bits ( setBit, clearBit ) -import Control.Monad ( liftM ) data UUID = UUID Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8 @@ -64,14 +63,16 @@ instance Show UUID where printf "%02x" o ++ printf "%02x" p -getRandomUUID :: IO UUID -getRandomUUID = do - let getRN :: a -> IO Word8 - getRN _ = liftM fromIntegral (randomIO :: IO Int) - [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p] <- mapM getRN ([1..16] :: [Int]) +getUUID :: RandomGen g => g -> UUID +getUUID gen = + let [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p] = take 16 $ randoms gen :: [Word8] -- set variant - let i' = i `setBit` 7 `clearBit` 6 + i' = i `setBit` 7 `clearBit` 6 -- set version (0100 for random) - let g' = g `clearBit` 7 `setBit` 6 `clearBit` 5 `clearBit` 4 - return $ UUID a b c d e f g' h i' j k l m n o p + g' = g `clearBit` 7 `setBit` 6 `clearBit` 5 `clearBit` 4 + in + UUID a b c d e f g' h i' j k l m n o p + +getRandomUUID :: IO UUID +getRandomUUID = getUUID <$> getStdGen |