From b66e9234fa0162a1035138891d88ad85f00950f6 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Sun, 15 Jan 2017 00:12:54 +1100 Subject: Improved style, reading file twice to count lines here to stay --- src/Counter.hs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'src') 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 -- cgit