git.iko.soy/typeable/pandoc
on master help sign in
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.

commit8b144db6e575e56205de881d8ae233fe2ff828da authorJesse Rosenthal <jrosenthal@jhu.edu> dateSat 19 Nov 2016, 05:31 (10 years ago) parent1c589c5
1 file changed+12−11
src/Text/Pandoc/UUID.hs+12−11
src/Text/Pandoc/UUID.hs+12−11
@@ -29,13 +29,12 @@ UUID generation using Version 4 (random method) described
2929in RFC4122. See http://tools.ietf.org/html/rfc4122
3030-}
3131
32−module Text.Pandoc.UUID ( UUID(..), getRandomUUID ) where
32+module Text.Pandoc.UUID ( UUID(..), getRandomUUID, getUUID ) where
3333
3434import Text.Printf ( printf )
35−import System.Random ( randomIO )
35+import System.Random ( RandomGen, randoms, getStdGen )
3636import Data.Word
3737import Data.Bits ( setBit, clearBit )
38−import Control.Monad ( liftM )
3938
4039data UUID = UUID Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8
4140 Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8
@@ -64,14 +63,16 @@ instance Show UUID where
6463 printf "%02x" o ++
6564 printf "%02x" p
6665
67−getRandomUUID :: IO UUID
68−getRandomUUID = do
69− let getRN :: a -> IO Word8
70− getRN _ = liftM fromIntegral (randomIO :: IO Int)
71− [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p] <- mapM getRN ([1..16] :: [Int])
66+getUUID :: RandomGen g => g -> UUID
67+getUUID gen =
68+ let [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p] = take 16 $ randoms gen :: [Word8]
7269 -- set variant
73− let i' = i `setBit` 7 `clearBit` 6
70+ i' = i `setBit` 7 `clearBit` 6
7471 -- set version (0100 for random)
75− let g' = g `clearBit` 7 `setBit` 6 `clearBit` 5 `clearBit` 4
76− return $ UUID a b c d e f g' h i' j k l m n o p
72+ g' = g `clearBit` 7 `setBit` 6 `clearBit` 5 `clearBit` 4
73+ in
74+ UUID a b c d e f g' h i' j k l m n o p
75+
76+getRandomUUID :: IO UUID
77+getRandomUUID = getUUID <$> getStdGen
7778