{-# OPTIONS -fbang-patterns #-} import System.Environment import Control.Monad import qualified Data.ByteString.Char8 as S import qualified Data.ByteString.Unsafe as S import qualified Data.IntMap as M main = do [f] <- getArgs x <- loadToMap f print (M.size x) getChar -- wait loadToMap :: String -> IO (M.IntMap S.ByteString) loadToMap f = parseLines `fmap` S.readFile f -- -- Build an IntMap as we traverse the NNN\tfoo\n lines of the file -- parseLines :: S.ByteString -> M.IntMap S.ByteString parseLines s = go s M.empty where go s m | S.null s = m | otherwise = case S.readInt s of Nothing -> error "No integer field" Just (n, y) -> go (S.tail rest) m' where (str,rest) = S.break ('\n'==) (S.unsafeTail y) m' = M.insert (fromIntegral n) str m