From e7dbb4348d17f44c0f9162bea68738f3e2dc72f8 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Thu, 26 Jan 2017 21:11:21 +1100 Subject: Moving some code to Miscellaneous --- src/Election.hs | 19 +++++-------------- src/Miscellaneous.hs | 23 ++++++++++++++++++++++- src/main.hs | 24 ++++++------------------ 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/Election.hs b/src/Election.hs index be4fe05..39bcb22 100644 --- a/src/Election.hs +++ b/src/Election.hs @@ -19,6 +19,7 @@ import qualified Data.Either.Unwrap as Either import qualified Counter as Sen import qualified Candidate as Typ import qualified CSV as CSV +import qualified Miscellaneous as Misc @@ -203,7 +204,7 @@ doElectCandidate :: Election -> ET.EitherT Election IO Election doElectCandidate e = do let (running, notRunning) = List.partition ((== Running) . getStatus) (getEntries e) electedEntry = List.maximumBy compareVotes running - (beforeEntries, afterEntries) = partBeforeAfter electedEntry (getEntries e) + (beforeEntries, afterEntries) = Misc.partBeforeAfter electedEntry (getEntries e) newTransferValue = (fromIntegral (getTotalVotes electedEntry - getQuota e)) / (fromIntegral (getTotalVotes electedEntry)) @@ -258,7 +259,7 @@ doVoteTransfer :: Election -> ET.EitherT Election IO Election doVoteTransfer e = do let (currentTransfer:remainingTransfers) = getTransferQueue e fromEntry = Maybe.fromJust (List.find ((== getWhoFrom currentTransfer) . getID) (getEntries e)) - (beforeEntries, afterEntries) = partBeforeAfter fromEntry (getEntries e) + (beforeEntries, afterEntries) = Misc.partBeforeAfter fromEntry (getEntries e) mapKeys = map getID (beforeEntries ++ afterEntries) notRunningKeys = map getID (filter ((/= Running) . getStatus) (getEntries e)) @@ -371,7 +372,7 @@ checkNoQuota :: Election -> ET.EitherT Election IO Election checkNoQuota e = do let (running, notRunning) = List.partition ((== Running) . getStatus) (getEntries e) minimumEntry = List.minimumBy compareVotes running - (beforeEntries, afterEntries) = partBeforeAfter minimumEntry (getEntries e) + (beforeEntries, afterEntries) = Misc.partBeforeAfter minimumEntry (getEntries e) makeElect x = x { getStatus = Elected @@ -401,7 +402,7 @@ excludeSomeone :: Election -> ET.EitherT Election IO Election excludeSomeone e = do let (running, notRunning) = List.partition ((== Running) . getStatus) (getEntries e) excludedEntry = List.minimumBy compareVotes running - (beforeEntries, afterEntries) = partBeforeAfter excludedEntry (getEntries e) + (beforeEntries, afterEntries) = Misc.partBeforeAfter excludedEntry (getEntries e) newTransfer = Transfer { getWhoFrom = getID excludedEntry @@ -422,16 +423,6 @@ excludeSomeone e = do -partBeforeAfter :: (Eq a) => a -> [a] -> ([a],[a]) -partBeforeAfter item list = - let (x,y) = List.break (== item) list - in if (length y <= 1) - then (x,[]) - else (x,tail y) - - - - compareVotes :: Entry -> Entry -> Ordering compareVotes x y = compare (getTotalVotes x) (getTotalVotes y) diff --git a/src/Miscellaneous.hs b/src/Miscellaneous.hs index 5a559a5..5f018e6 100644 --- a/src/Miscellaneous.hs +++ b/src/Miscellaneous.hs @@ -1,7 +1,9 @@ module Miscellaneous( if', (?), - selectFrom + selectFrom, + readMaybe, + partBeforeAfter ) where @@ -45,3 +47,22 @@ selectFrom pick has from = in Con.foldM foldFunc [] (zip [1,2..] from) + + +readMaybe :: Read a => String -> Maybe a +readMaybe s = + case reads s of + [(val, "")] -> Just val + _ -> Nothing + + + + +partBeforeAfter :: (Eq a) => a -> [a] -> ([a],[a]) +partBeforeAfter item list = + let (x,y) = List.break (== item) list + in if (length y <= 1) + then (x,[]) + else (x,tail y) + + diff --git a/src/main.hs b/src/main.hs index 8cec703..4059d3b 100644 --- a/src/main.hs +++ b/src/main.hs @@ -10,6 +10,7 @@ import qualified Data.Maybe as Maybe import qualified Counter as Sen import qualified Candidate as Cand import qualified Election as Elt +import qualified Miscellaneous as Misc @@ -41,18 +42,9 @@ defaultOptions = Options -readMaybe :: Read a => String -> Maybe a -readMaybe s = - case reads s of - [(val, "")] -> Just val - _ -> Nothing - - - - electOpt :: String -> (Options -> Options) electOpt str = - let r = readMaybe str :: Maybe Int + let r = Misc.readMaybe str :: Maybe Int jr = if (Maybe.isJust r && Maybe.fromJust r > 0) then r else Nothing in (\opts -> opts { getNumToElect = jr }) @@ -73,6 +65,10 @@ optionHeader = "Note that the -c, -p, -o, -e, -s options are all\n" ++ "required for normal operation.\n" +furtherHelp = + "Please be sure to provide all required options to run the election counter.\n" ++ + "For further information consult '--help'.\n" + optionData :: [Opt.OptDescr (Options -> Options)] optionData = [ Opt.Option ['v'] ["verbose"] @@ -119,14 +115,6 @@ getOpts argv = -furtherHelp :: String -furtherHelp = - "Please be sure to provide all required options to run the election counter.\n" ++ - "For further information consult '--help'.\n" - - - - main = do rawArgs <- Env.getArgs (options, arguments) <- getOpts rawArgs -- cgit