Created
June 13, 2014 05:58
-
-
Save GrooveStomp/7789bb8faf59ef91e6c0 to your computer and use it in GitHub Desktop.
Yorgey class - week 2 exercise
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 #-} | |
| module LogAnalysis where | |
| import Log | |
| import qualified Data.Text as T | |
| import Control.Monad | |
| import Data.Char | |
| parseMessage :: String -> LogMessage | |
| parseMessage = parseMessage' . T.split (==' ') . T.pack | |
| parseMessage' :: [T.Text] -> LogMessage | |
| parseMessage' pieces | |
| | (=='I') label = LogMessage Info timestamp (T.unpack $ rejoin rest) | |
| | (=='W') label = LogMessage Warning timestamp (T.unpack $ rejoin rest) | |
| | (=='E') label = LogMessage (Error my_error) maybe_timestamp (T.unpack $ rejoin maybe_rest) | |
| | otherwise = Unknown (T.unpack $ rejoin pieces) | |
| where | |
| label = head . T.unpack . head $ pieces | |
| timestamp = parseInt (head . tail $ pieces) :: Int | |
| my_error = timestamp | |
| rest = tail . tail $ pieces | |
| maybe_timestamp = parseInt (head rest) :: Int | |
| maybe_rest = tail rest | |
| parseInt :: T.Text -> Int | |
| parseInt = read . T.unpack | |
| rejoin :: [T.Text] -> T.Text | |
| rejoin = T.intercalate (T.pack " ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment