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