Skip to content

Instantly share code, notes, and snippets.

@beeleebow
Last active July 27, 2018 04:08
Show Gist options
  • Save beeleebow/2c5a93583b98ca1a8f5d9871a7f11281 to your computer and use it in GitHub Desktop.
Save beeleebow/2c5a93583b98ca1a8f5d9871a7f11281 to your computer and use it in GitHub Desktop.
cis194 w02 solution
-- CIS 194 Homework 2
module Week02.Log
( MessageType(..)
, TimeStamp
, LogMessage(..)
, MessageTree(..)
, testParse
, testWhatWentWrong
) 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
module Week02.LogAnalysis
( parseMessage
, parse
, insert
, build
, inOrder
, whatWentWrong
, module Week02.Log
) where
import Week02.Log
( LogMessage(..)
, MessageTree(..)
, MessageType(..)
, TimeStamp
, testWhatWentWrong
, testParse
)
parseMessage :: String -> LogMessage
parseMessage s = case words s of
"E":l:t:r -> LogMessage (Error (read l)) (read t) (unwords r)
"I":t:r -> LogMessage Info (read t) (unwords r)
"W":t:r -> LogMessage Warning (read t) (unwords r)
ws -> Unknown (unwords ws)
parse :: String -> [LogMessage]
parse s =
let
accumulate [] = []
accumulate (x:xs) = parseMessage x : accumulate xs
in
accumulate (lines s)
-- ************************************* Fancy ******************************************
-- parse = map parseMessage . lines
insert :: LogMessage -> MessageTree -> MessageTree
insert (Unknown _) t = t
insert x Leaf = Node Leaf x Leaf
insert x@(LogMessage _ t1 _) t@(Node lt y@(LogMessage _ t2 _) rt)
| t1 < t2 = Node (insert x lt) y rt
| otherwise = Node lt y (insert x rt)
build :: [LogMessage] -> MessageTree
build xs =
let
accumulate ys tree = case ys of
[] -> tree
(z:zs) -> accumulate zs (insert z tree)
in
accumulate xs Leaf
-- ************************************* Fancy ******************************************
-- build = foldr insert Leaf
inOrder :: MessageTree -> [LogMessage]
inOrder Leaf = []
inOrder (Node Leaf x rt) = x : inOrder rt
inOrder (Node lt x rt) = inOrder lt ++ [x] ++ inOrder rt
whatWentWrong :: [LogMessage] -> [String]
whatWentWrong xs =
let
isSevere (LogMessage (Error n) _ _) = n >= 50
isSevere _ = False
accumulate [] = []
accumulate (y@(LogMessage _ _ m):ys) =
if isSevere y
then m : accumulate ys
else accumulate ys
in
accumulate (inOrder (build xs))
-- ************************************* Fancy ******************************************
-- whatWentWrong =
-- let
-- isSevere (LogMessage (Error n) _ _) = n >= 50
-- isSevere _ = False
-- getMsg (LogMessage _ _ m) = m
-- in
-- map getMsg . filter isSevere . inOrder . build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment