Skip to content

Instantly share code, notes, and snippets.

@nrdmn
Created March 24, 2019 16:42
Show Gist options
  • Save nrdmn/9ab36e302c2ae26f7017d2c8e3ee68d4 to your computer and use it in GitHub Desktop.
Save nrdmn/9ab36e302c2ae26f7017d2c8e3ee68d4 to your computer and use it in GitHub Desktop.
import qualified Data.HashMap.Strict as M
import qualified Data.ByteString.Char8 as B
import Data.List (sortOn)
import Data.List.Split (chunksOf)
import Control.Parallel.Strategies (parMap, rdeepseq)
main = do
words <- B.words <$> B.getContents
let result = sortOn snd $ M.toList $ foldl (M.unionWith (+)) M.empty $ parMap rdeepseq (foldl (\acc x -> M.insertWith (+) x (1 :: Integer) acc) M.empty) (chunksOf 50000 words)
B.putStr . B.concat $ result >>= (\(a,b) -> [B.pack $ show b, B.singleton '\t', a, B.singleton '\n'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment