public
Created

Haskell Iteratees Example

  • Download Gist
IterateesHomework.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.