Switch from Map to List
A lot faster than before!
This commit is contained in:
parent
ac5831f063
commit
6b28ba7258
@ -1,35 +1,29 @@
|
|||||||
module Alea.Diceware where
|
module Alea.Diceware where
|
||||||
|
|
||||||
import qualified Data.Map as Map
|
import Data.List (intersect, elemIndex)
|
||||||
|
import Data.Maybe (fromJust)
|
||||||
import Alea.List
|
import Alea.List
|
||||||
|
|
||||||
-- Diceware dictionary type
|
-- Diceware dictionary type
|
||||||
type Diceware = Map.Map Int String
|
type Diceware = [String]
|
||||||
|
|
||||||
-- Parse a line into a tuple
|
|
||||||
parseLine :: String -> (Int, String)
|
|
||||||
parseLine x =
|
|
||||||
(read a :: Int, b)
|
|
||||||
where
|
|
||||||
x' = split x ' '
|
|
||||||
(a, b) = (head x', last x')
|
|
||||||
|
|
||||||
-- Parse file content to a Diceware
|
-- Parse file content to a Diceware
|
||||||
parseDiceware :: String -> Diceware
|
parseDiceware :: String -> Diceware
|
||||||
parseDiceware x = (Map.fromList . map parseLine . lines) x
|
parseDiceware x = map (last . split ' ') $ lines x
|
||||||
|
|
||||||
-- Lookup word with dice index
|
-- Lookup word with dice index
|
||||||
readDiceware :: Diceware -> Int -> String
|
readDiceware :: Diceware -> Int -> String
|
||||||
readDiceware d n =
|
readDiceware d n = show n ++ " -> " ++
|
||||||
show n ++ " -> " ++
|
case (undice n) of
|
||||||
case Map.lookup n d of
|
Just x -> d !! x
|
||||||
Just x -> x
|
|
||||||
Nothing -> "Does not exist"
|
Nothing -> "Does not exist"
|
||||||
|
|
||||||
-- Lookup word with linear index
|
-- Lookup word with linear index
|
||||||
readDiceware' :: Diceware -> Int -> String
|
readDiceware' :: Diceware -> Int -> String
|
||||||
readDiceware' d n = (snd . (!!n) . Map.toList) d
|
readDiceware' d n = d !! n
|
||||||
|
|
||||||
-- Size of Diceware (should be 2^5)
|
-- Dice numbers to numbers
|
||||||
size :: Diceware -> Int
|
-- Ex. undice 11121 == Just 6
|
||||||
size = Map.size
|
undice :: Int -> Maybe Int
|
||||||
|
undice n = elemIndex n . filter
|
||||||
|
(null . (intersect "0789") . show) $ [11111..66666]
|
||||||
|
Loading…
Reference in New Issue
Block a user