Skip to content

Instantly share code, notes, and snippets.

@willtim
Created December 6, 2011 09:27
Show Gist options
  • Save willtim/1437532 to your computer and use it in GitHub Desktop.
Save willtim/1437532 to your computer and use it in GitHub Desktop.
Haskell Iteratees Example
module IterateesHomework where
import Prelude hiding (words, unwords, init)
import Data.Text hiding (reverse, map, take)
import Data.Enumerator hiding (map)
import Control.Applicative ((<$>))
import Data.List (sort)
import qualified Data.Enumerator.Binary as EB
import qualified Data.Enumerator.Text as ET
import qualified Data.Enumerator.List as EL
import qualified Data.Map as M
data Entry = Entry {
date :: Text,
host :: Text,
program :: Text,
message :: Text
} deriving Show
type Histogram = M.Map Text Int
main = do
let enumEntries = ET.enumFile "/var/log/syslog" $= EL.map parse
s <- (run_ $ enumEntries $$ processEntry)
print $ s
parse :: Text -> Entry
parse s = Entry d h p m
where d1:d2:d3:h:p':ms = words s
d = unwords [d1,d2,d3]
p = init p'
m = unwords ms
processEntry :: Monad m => Iteratee Entry m ([(Int, Text)], Int)
processEntry = (top10 <$> summary) `EL.zip` countLines
summary :: Monad m => Iteratee Entry m Histogram
summary = EL.fold op M.empty
where op :: Histogram -> Entry -> Histogram
op m e = M.insertWith' (+) (program e) 1 m
countLines :: Monad m => Iteratee Entry m Int
countLines = EL.fold (flip $ const (+1)) 0
top10 :: Histogram -> [(Int, Text)]
top10 = take 10 . reverse . sort . map swap . M.toList
swap (x,y) = (y, x) -- not in Data.Tuple
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment