Created
September 6, 2014 23:55
-
-
Save Rydgel/dfbfc5b5922f22a56aa5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# OPTIONS_GHC -Wall #-} | |
| -- Homework 2 - http://www.seas.upenn.edu/~cis194/spring13/hw/02-ADTs.pdf | |
| module LogAnalysis where | |
| import Log | |
| parseMessage :: String -> LogMessage | |
| parseMessage = parseWords . words | |
| where parseWords :: [String] -> LogMessage | |
| parseWords ("I":(x:xs)) = LogMessage Info (read x :: Int) (unwords xs) | |
| parseWords ("W":(x:xs)) = LogMessage Warning (read x :: Int) (unwords xs) | |
| parseWords ("E":(x:(y:xs))) = LogMessage (Error (read x :: Int)) (read y :: Int) (unwords xs) | |
| parseWords xs = Unknown (unwords xs) | |
| parse :: String -> [LogMessage] | |
| parse = map parseMessage . lines | |
| getTimestamp :: LogMessage -> TimeStamp | |
| getTimestamp (Unknown _) = error "No timestamp in this shit!" | |
| getTimestamp (LogMessage _ timestamp _) = timestamp | |
| insert :: LogMessage -> MessageTree -> MessageTree | |
| insert (Unknown _) tree = tree | |
| insert msgToInsert Leaf = Node Leaf msgToInsert Leaf | |
| insert msgToInsert (Node left rootMsg right) | |
| | insertTimestamp <= rootTimestamp = Node (insert msgToInsert left) rootMsg right | |
| | otherwise = Node left rootMsg (insert msgToInsert right) | |
| where insertTimestamp = getTimestamp msgToInsert | |
| rootTimestamp = getTimestamp rootMsg | |
| build :: [LogMessage] -> MessageTree | |
| build = foldr insert Leaf | |
| inOrder :: MessageTree -> [LogMessage] | |
| inOrder Leaf = [] | |
| inOrder (Node left logMsg right) = inOrder left ++ [logMsg] ++ inOrder right | |
| getString :: LogMessage -> String | |
| getString (Unknown s) = s | |
| getString (LogMessage _ _ s) = s | |
| filterBigError :: LogMessage -> Bool | |
| filterBigError (LogMessage (Error x) _ _) = if x > 50 then True else False | |
| filterBigError _ = False | |
| whatWentWrong :: [LogMessage] -> [String] | |
| whatWentWrong = map getString . filter filterBigError . inOrder . build |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment