|
| 1 | +module ParserCombinators.ChapterExercises.LogFile where |
| 2 | + |
| 3 | +import Control.Applicative |
| 4 | +import Data.List |
| 5 | +import Text.Trifecta |
| 6 | + |
| 7 | +instance (Eq a) => Eq (Result a) where |
| 8 | + Success a == Success b = a == b |
| 9 | + Failure a == Failure b = True |
| 10 | + _ == _ = False |
| 11 | + |
| 12 | +-- Some types |
| 13 | +type Year = Integer |
| 14 | +type Month = Integer |
| 15 | +type Day = Integer |
| 16 | + |
| 17 | +data Date = Date Year Month Day |
| 18 | + deriving (Show, Eq) |
| 19 | + |
| 20 | +type Hour = Integer |
| 21 | +type Minute = Integer |
| 22 | + |
| 23 | +data Time = Time Hour Minute |
| 24 | + deriving (Show, Eq) |
| 25 | + |
| 26 | +type StartTime = Time |
| 27 | +type Name = String |
| 28 | +type Duration = Double |
| 29 | + |
| 30 | +data Activity = Activity StartTime Name |
| 31 | + deriving (Show, Eq) |
| 32 | + |
| 33 | +data Daily = Daily Date [Activity] |
| 34 | + deriving (Show, Eq) |
| 35 | + |
| 36 | +-- Parsing |
| 37 | +comment :: Parser String |
| 38 | +comment = try (spaces >> string "--" >> many (noneOf "\n")) |
| 39 | + |
| 40 | +eol :: Parser () |
| 41 | +eol = (newline >> return ()) <|> (comment >> return ()) |
| 42 | + |
| 43 | +-- This function is tricky |
| 44 | +-- We have to 1) ignore comments started with -- and all white spaces before |
| 45 | +-- 2) handle correctly a single dash |
| 46 | +parseName :: Parser String |
| 47 | +parseName = (eof >> return "") |
| 48 | + <|> (eol >> return "") |
| 49 | + <|> (liftA2 (:) anyChar parseName) |
| 50 | + |
| 51 | +parseDate :: Parser Date |
| 52 | +parseDate = do |
| 53 | + string "# " |
| 54 | + year <- integer |
| 55 | + char '-' |
| 56 | + month <- integer |
| 57 | + char '-' |
| 58 | + day <- integer |
| 59 | + many newline |
| 60 | + return $ Date year month day |
| 61 | + |
| 62 | +parseTime :: Parser Time |
| 63 | +parseTime = do |
| 64 | + hour <- integer |
| 65 | + char ':' |
| 66 | + minute <- integer |
| 67 | + return $ Time hour minute |
| 68 | + |
| 69 | +parseActivity :: Parser Activity |
| 70 | +parseActivity = do |
| 71 | + startTime <- parseTime |
| 72 | + -- integer consumes the trailing zero |
| 73 | + -- so `_ <- char ' '` gives an error |
| 74 | + skipMany (char ' ') |
| 75 | + name <- parseName |
| 76 | + skipMany eol |
| 77 | + return $ Activity startTime name |
| 78 | + |
| 79 | +parseDaily :: Parser Daily |
| 80 | +parseDaily = do |
| 81 | + date <- parseDate |
| 82 | + skipMany eol |
| 83 | + activities <- some parseActivity |
| 84 | + return $ Daily date activities |
| 85 | + |
| 86 | +parseLog :: Parser [Daily] |
| 87 | +parseLog = skipMany eol >> some parseDaily |
| 88 | + |
| 89 | +-- Group by the first component and aggregate the second component |
| 90 | +-- with a binary operator |
| 91 | +groupByOp :: Eq a => (b -> b -> b) -> [(a, b)] -> [(a, b)] |
| 92 | +groupByOp op = map reduce . groupBy (\a a' -> fst a == fst a') |
| 93 | + where reduce = liftA2 (,) (head . map fst) (foldr1 op . map snd) |
| 94 | + |
| 95 | +dailySummary :: Daily -> [(Name, Duration)] |
| 96 | +dailySummary (Daily _ activities) = nameDurations |
| 97 | + where getDuration (Activity t1 n1) (Activity t2 _) = (n1, timeDiff t1 t2) |
| 98 | + timeDiff (Time h1 m1) (Time h2 m2) = fromIntegral $ (h2 * 60 + m2) - (h1 * 60 + m1) |
| 99 | + nameDurations = groupByOp (+) $ sortOn fst $ zipWith getDuration activities (tail activities) |
| 100 | + |
| 101 | +summary :: [Daily] -> [(Name, Duration)] |
| 102 | +summary = groupByOp (+) . sortOn fst . concatMap dailySummary |
| 103 | + |
| 104 | +-- Average time spent on each activity |
| 105 | +avgTime :: [Daily] -> [(Name, Duration)] |
| 106 | +avgTime xs = map (\(name, duration) -> (name, duration / len)) $ summary xs |
| 107 | + where len = fromIntegral $ length xs |
0 commit comments