Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion haskell-version/haskell-version.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 7 additions & 14 deletions haskell-version/src/Main.hs
Original file line number Diff line number Diff line change
@@ -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"