on master help sign in
Use an IORef for the counts in hashmap
349c9d7Harendra Kumar
examples/WordClassifier.hs+7−3
@@ -12,6 +12,7 @@ import Data.Function ((&))
12import Data.Functor.Identity (Identity(..))
12import Data.Functor.Identity (Identity(..))
13import qualified Data.HashMap.Strict as Map
13import qualified Data.HashMap.Strict as Map
14import Data.Hashable
14import Data.Hashable
15+import Data.IORef
15import qualified Data.List as List
16import qualified Data.List as List
16import qualified Data.Ord as Ord
17import qualified Data.Ord as Ord
17import Foreign.Storable (Storable(..))
18import Foreign.Storable (Storable(..))
@@ -48,7 +49,8 @@ isAlpha c
48main :: IO ()
49main :: IO ()
49main =
50main =
50 let
51 let
51− increment m str = Map.insertWith (+) str (1 :: Int) m
52+ alter Nothing = fmap Just $ newIORef (1 :: Int)
53+ alter (Just ref) = modifyIORef' ref (+ 1) >> return (Just ref)
52 in do
54 in do
53 name <- fmap head getArgs
55 name <- fmap head getArgs
54 src <- openFile name ReadMode
56 src <- openFile name ReadMode
@@ -57,6 +59,8 @@ main =
57 & Streamly.map toLower -- SerialT IO Char
59 & Streamly.map toLower -- SerialT IO Char
58 & Streamly.foldWords FL.toList -- SerialT IO String
60 & Streamly.foldWords FL.toList -- SerialT IO String
59 & Streamly.filter (all isAlpha)
61 & Streamly.filter (all isAlpha)
60− & Streamly.foldl' increment Map.empty
62+ & Streamly.foldlM' (flip (Map.alterF alter)) Map.empty
63+ & fmap Map.toList
64+ >>= mapM (\(w, ref) -> readIORef ref >>= \cnt -> return (w, cnt))
61− & fmap (List.take 25 . List.sortOn (Ord.Down . snd) . Map.toList)
65+ & fmap (List.take 25 . List.sortOn (Ord.Down . snd))
62 >>= traverse_ print
66 >>= traverse_ print