From df7c097fa8d69ee6315bc4a315095b677b6050fb Mon Sep 17 00:00:00 2001 From: klapaucius Date: Wed, 2 Aug 2017 00:54:12 +0500 Subject: [PATCH] IntMap -> Vector --- haskell-version/haskell-version.cabal | 3 ++- haskell-version/src/Main.hs | 21 +++++++-------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/haskell-version/haskell-version.cabal b/haskell-version/haskell-version.cabal index cc0b33c..d5f59b9 100644 --- a/haskell-version/haskell-version.cabal +++ b/haskell-version/haskell-version.cabal @@ -16,8 +16,9 @@ extra-source-files: README.md executable haskell-version hs-source-dirs: src main-is: Main.hs + ghc-options: -O2 default-language: Haskell2010 build-depends: base >= 4.7 && < 5 , bytestring >= 0.10.8.1 , bytestring-lexing - , containers >= 0.5.7.1 + , vector diff --git a/haskell-version/src/Main.hs b/haskell-version/src/Main.hs index 490e956..3f37014 100644 --- a/haskell-version/src/Main.hs +++ b/haskell-version/src/Main.hs @@ -1,23 +1,16 @@ module Main where import qualified Data.ByteString.Char8 as C8 -import qualified Data.IntMap.Strict as M -import Data.List import Data.ByteString.Lex.Integral +import qualified Data.Vector.Unboxed as V toInt :: C8.ByteString -> Int toInt = readDecimal_ -processFile = do - bsLines <- C8.lines <$> C8.readFile "../ngrams.tsv" - let info = C8.splitWith (=='\t') <$> bsLines :: [[C8.ByteString]] - let m = foldl' - (\acc x -> let (_:key:val:_) = (x :: [C8.ByteString]) in M.insertWith (\new old -> old + new) (toInt key) (toInt val) acc) - M.empty - info - pure $ last $ sortOn snd (M.toList m) +process bs = (i, xs V.! i) where + info = C8.splitWith (=='\t') <$> C8.lines bs + xs = V.unsafeAccumulate (+) (V.replicate 2009 0) . V.fromList + $ map (\(_:key:val:_) -> (toInt key, toInt val)) info + i = V.maxIndex xs -main :: IO () -main = do - x <- processFile - print x +main = print . process =<< C8.readFile "../ngrams.tsv"