summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2017-01-15 00:12:54 +1100
committerJed Barber <jjbarber@y7mail.com>2017-01-15 00:12:54 +1100
commitb66e9234fa0162a1035138891d88ad85f00950f6 (patch)
tree4681ea05622f25d24902c41c41175ab1e4d5c93b
parentb7bcf8a3377bc3b847234baaf24fe6e46d7f85b7 (diff)
Improved style, reading file twice to count lines here to stay
-rw-r--r--src/Counter.hs12
1 files changed, 5 insertions, 7 deletions
diff --git a/src/Counter.hs b/src/Counter.hs
index 48429ef..021ac45 100644
--- a/src/Counter.hs
+++ b/src/Counter.hs
@@ -37,10 +37,9 @@ data SenateCounter = SenateCounter
createSenateCounter :: FilePath -> Typ.AboveLineBallot -> Typ.BelowLineBallot -> IO SenateCounter
createSenateCounter f a b = do
- --
numLines <- File.countLines f
arrayData <- Vec.createStore numLines (length b)
- --
+
h <- IO.openFile f IO.ReadMode
let readFunc n p = if (n > numLines) then return p else do
t0 <- IO.hGetLine h
@@ -54,7 +53,7 @@ createSenateCounter f a b = do
else readFunc (n + 1) p
p <- readFunc 1 0
IO.hClose h
- --
+
return (SenateCounter arrayData b p)
@@ -62,15 +61,14 @@ createSenateCounter f a b = do
doCount :: SenateCounter -> Criteria -> IO Int
doCount sen criteria = do
- --
let isValidCriteria = all (\(x,y) -> y `List.elem` (ballotMap sen)) criteria
- --
+
let critToPref (a,b) = (Maybe.fromJust (List.elemIndex b (ballotMap sen)) + 1, a)
neededPrefs = map critToPref criteria
- checkFunc n r = if (n > (numBallots sen)) then return r else do
+ checkFunc n r = if (n > numBallots sen) then return r else do
t <- Con.liftM and (mapM (Vec.checkPref (prefData sen) n) neededPrefs)
if t then checkFunc (n + 1) (r + 1) else checkFunc (n + 1) r
- --
+
if isValidCriteria
then checkFunc 1 0
else return 0