-
-
Save joneshf/77ab2f0598185a284993 to your computer and use it in GitHub Desktop.
This file contains 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
-- CIS 194 Homework 2 | |
module Log where | |
import Control.Applicative | |
data MessageType = Info | |
| Warning | |
| Error Int | |
deriving (Show, Eq) | |
type TimeStamp = Int | |
data LogMessage = LogMessage MessageType TimeStamp String | |
| Unknown String | |
deriving (Show, Eq) | |
data MessageTree = Leaf | |
| Node MessageTree LogMessage MessageTree | |
deriving (Show, Eq) | |
-- | @testParse p n f@ tests the log file parser @p@ by running it | |
-- on the first @n@ lines of file @f@. | |
testParse :: (String -> [LogMessage]) | |
-> Int | |
-> FilePath | |
-> IO [LogMessage] | |
testParse parse n file = take n . parse <$> readFile file | |
-- | @testWhatWentWrong p w f@ tests the log file parser @p@ and | |
-- warning message extractor @w@ by running them on the log file | |
-- @f@. | |
testWhatWentWrong :: (String -> [LogMessage]) | |
-> ([LogMessage] -> [String]) | |
-> FilePath | |
-> IO [String] | |
testWhatWentWrong parse whatWentWrong file | |
= whatWentWrong . parse <$> readFile file |
This file contains 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
module LogAnalysis where | |
import Log | |
parseMessage :: String -> LogMessage | |
parseMessage s = LogMessage messageType timestamp message | |
where wordAry = words s | |
firstChar = wordAry !! 0 | |
secondChar = wordAry !! 1 | |
messageType = case firstChar of | |
"I" -> Info | |
"W" -> Warning | |
"E" -> (Error (read secondChar :: Int)) | |
timestamp = case messageType of | |
(Error n) -> read (wordAry !! 2) :: Int | |
(Info) -> read (wordAry !! 1) :: Int | |
(Warning) -> read (wordAry !! 1) :: Int | |
message = case messageType of | |
(Error n) -> unwords (drop 3 wordAry) | |
(Info) -> unwords (drop 2 wordAry) | |
(Warning) -> unwords (drop 2 wordAry) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment