diff --git a/src/Breve/Generator.hs b/src/Breve/Generator.hs index 5a00861..122d8a3 100644 --- a/src/Breve/Generator.hs +++ b/src/Breve/Generator.hs @@ -16,22 +16,22 @@ import Data.Text.Encoding (encodeUtf8) type Name = Text type Url = Text --- Choose a random element of a list +-- | Takes 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 +-- | Generates 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 +-- | SHA256 hash to seed a generator intHash :: Url -> Int intHash = decode . fromStrict . hash . encodeUtf8 --- Assign a unique name to the url +-- | Assigns a unique name to the url nameHash :: Url -> Name nameHash = evalState word . mkStdGen . intHash diff --git a/src/Breve/Settings.hs b/src/Breve/Settings.hs index 68474d3..db9dea3 100644 --- a/src/Breve/Settings.hs +++ b/src/Breve/Settings.hs @@ -1,5 +1,3 @@ -{-# LANGUAGE OverloadedStrings #-} - module Breve.Settings where import Control.Monad (when) diff --git a/src/Breve/UrlTable.hs b/src/Breve/UrlTable.hs index 5ffb3f1..7ffb552 100644 --- a/src/Breve/UrlTable.hs +++ b/src/Breve/UrlTable.hs @@ -14,14 +14,14 @@ import qualified Data.HashTable.IO as H type UrlTable = H.CuckooHashTable Name Url --- Periodically write a url table to a file +-- | Periodically write a url table to a file sync :: UrlTable -> FilePath -> IO () sync table file = forever $ do threadDelay (round 3.0e8) content <- show <$> H.toList table writeFile file content --- Load a url table from a file +-- | Load a url table from a file load :: FilePath -> IO UrlTable load file = do content <- readFile file @@ -31,11 +31,11 @@ load file = do forkIO (sync table file) return table --- Insert the url in a table and return the name +-- | Insert the url in a table and return the name insert :: UrlTable -> Url -> IO Name insert table url = H.insert table new url >> return new where new = nameHash url --- Lookup a table for the associated url +-- | Lookup a table for the associated url extract :: UrlTable -> Name -> IO (Maybe Url) extract = H.lookup