alea/Alea/Diceware.hs
Rnhmjoj 6b28ba7258 Switch from Map to List
A lot faster than before!
2014-10-16 23:19:02 +02:00

30 lines
753 B
Haskell

module Alea.Diceware where
import Data.List (intersect, elemIndex)
import Data.Maybe (fromJust)
import Alea.List
-- Diceware dictionary type
type Diceware = [String]
-- Parse file content to a Diceware
parseDiceware :: String -> Diceware
parseDiceware x = map (last . split ' ') $ lines x
-- Lookup word with dice index
readDiceware :: Diceware -> Int -> String
readDiceware d n = show n ++ " -> " ++
case (undice n) of
Just x -> d !! x
Nothing -> "Does not exist"
-- Lookup word with linear index
readDiceware' :: Diceware -> Int -> String
readDiceware' d n = d !! n
-- Dice numbers to numbers
-- Ex. undice 11121 == Just 6
undice :: Int -> Maybe Int
undice n = elemIndex n . filter
(null . (intersect "0789") . show) $ [11111..66666]