module Breve.Generator ( nameHash , intHash , Name , Url ) where import Control.Monad.State import System.Random import Crypto.Hash.SHA256 (hash) import Data.Binary (decode) import Data.ByteString.Lazy (fromStrict) import Data.Text (Text, pack) import Data.Text.Encoding (encodeUtf8) type Name = Text type Url = Text -- Choose a random element of a list choice :: [a] -> State StdGen a choice xs = (xs !!) <$> randomSt (0, length xs - 1) where randomSt = state . randomR -- Generate a random phonetic string word :: State StdGen Name word = pack <$> replicateM 10 letter where vowels = "aeiou" consonants = "bcdfghjklmnpqrstvwxyz" letter = choice [vowels, consonants] >>= choice -- SHA256 hash to seed a generator intHash :: Url -> Int intHash = decode . fromStrict . hash . encodeUtf8 -- Assign a unique name to the url nameHash :: Url -> Name nameHash = evalState word . mkStdGen . intHash