Skip to content

Instantly share code, notes, and snippets.

@GrooveStomp
Created June 13, 2014 05:58
Show Gist options
  • Select an option

  • Save GrooveStomp/7789bb8faf59ef91e6c0 to your computer and use it in GitHub Desktop.

Select an option

Save GrooveStomp/7789bb8faf59ef91e6c0 to your computer and use it in GitHub Desktop.
Yorgey class - week 2 exercise
{-# 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