-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmicroscope.hs
More file actions
49 lines (39 loc) · 1.44 KB
/
microscope.hs
File metadata and controls
49 lines (39 loc) · 1.44 KB
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 Main where
import Data.List
import qualified Data.ByteString.Lazy as B hiding (pack, putStrLn, concatMap)
import qualified Data.ByteString.Lazy.Char8 as B hiding (head, cons, snoc)
import qualified Data.Map.Strict as M
import Data.Maybe
import Control.Applicative
import Data.Monoid
type PData = M.Map B.ByteString (M.Map B.ByteString Integer)
parse :: PData -> B.ByteString -> PData
parse m x = M.insertWith f parseIP parseMap m
where
(parseIP:_) = parseWords
parsePage = parseWords !! 10
parseWords = B.words x
parseMap
| isNothing lMap = M.fromList [(parsePage, 1)]
| otherwise = fromJust lMap
lMap = M.lookup parseIP m
f _ = M.insertWith (+) parsePage 1
folded :: [B.ByteString] -> PData
folded = foldl' parse blank
blank :: PData
blank = M.fromList [(B.empty, M.fromList [(B.empty, 0)])]
pretty :: PData -> [B.ByteString]
pretty = map pretty' . M.toList
where
pretty' (y, ys) = mconcat [ y, B.pack " has visited these pages:\n"
, mconcat . map printer . M.toList $ ys]
printer (u, v) = mconcat [ B.pack " ["
, B.pack . show $ v
, pluralView v
, u
, B.pack "\n" ]
pluralView :: Integer -> B.ByteString
pluralView 1 = B.pack " View] "
pluralView _ = B.pack " Views] "
main :: IO ()
main = mapM_ B.putStrLn =<< pretty . folded . B.lines <$> B.getContents