Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Log Analysis
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)
@cj3kim

This comment has been minimized.

Copy link
Owner Author

commented Jun 24, 2015

-- 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

@tvestelind

This comment has been minimized.

Copy link

commented Jun 24, 2015

Maybe this could be a cleaner solution but I'm not sure that it works, I haven't compiled it or anything...

parseMessage :: String -> LogMessage
parseMessage ('I' : t : m) = LogMessage Info t m
parseMessage ('W' : t : m) = LogMessage Warning t m
parseMessage ('E' : e : t : m) = LogMessage (Error e) t m

@cj3kim

This comment has been minimized.

Copy link
Owner Author

commented Jun 24, 2015

use pattern matching
bemkap has joined (~bemkap@host128.190-136-202.telecom.net.ar)
whiteline
but use -Wall so you don't forget to add defaults
jonmorehouse has left IRC (Ping timeout: 246 seconds)
mtakkman has left IRC (Ping timeout: 256 seconds)
tedkornish
Since you're not reusing firstChar, you can inline it
joneshf-laptop
cj3kim, This isn't really a refactoring comment, but what about the failure case: Unknown?
tedkornish
So you can say case head wordAry of
Natch has left IRC (Ping timeout: 252 seconds)
mmachenry1 has joined (~Adium@208.78.140.193)
kadoban has joined (~mud@unaffiliated/kadoban)
cj3kim
ah, good point
Welkin
cj3kim: use words on the input, the pattern match on the three cases
11:08 Welkin
call unwords on the message

@cj3kim

This comment has been minimized.

Copy link
Owner Author

commented Jun 24, 2015

okay, I will look more into pattern matching
husanu has left IRC (Remote host closed the connection)
reindeernix
why shouldn't you use !!?
husanu has joined (~husanu@93.114.40.115)
joneshf-laptop
partial
obb has left IRC (Remote host closed the connection)
cj3kim
and then come back when I'm done
whiteline
well, it's ugly. also, you're traversing a structure twice.
machine_coder has joined (~machine_c@192-0-223-82.cpe.teksavvy.com)
joneshf-laptop

[1,2,3] !! 10
lambdabot
*Exception: Prelude.!!: index too large
dan_f has joined (~dan@wireless-165-124-144-153.nuwlan.northwestern.edu)
whiteline
no idea if the compiler optimizes that away though
r4yan2 has left IRC (Ping timeout: 264 seconds)
whiteline
yeah, and patterns can catch size issues w/otherwise
vkosdll has joined (68833a8a@gateway/web/freenode/ip.104.131.58.138)
tristanp has left IRC (Remote host closed the connection)
Welkin
parseMessage i = case words i of ('E' : code : line : msg) -> LogMessage (Error code) line (words msg)
Welkin
er
Welkin
unwords msg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.