alea/Alea/Diceware.hs

29 lines
724 B
Haskell
Raw Normal View History

2014-10-14 18:09:58 +02:00
module Alea.Diceware where
2014-10-13 21:26:55 +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
type Diceware = [String]
2014-10-13 21:26:55 +02:00
-- Parse file content to a Diceware
parseDiceware :: String -> Diceware
parseDiceware x = map (last . split ' ') $ lines x
2014-10-13 21:26:55 +02:00
-- Lookup word with dice index
readDiceware :: Diceware -> Int -> String
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
readDiceware' d n = d !! n
2014-10-13 21:26:55 +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]