on master help sign in
Make toLower and isAlpha more efficient
commit6f3fda8a4a1d850587998091facd557437eb17d2 authorHarendra Kumar <harendra.kumar@gmail.com> dateFri 6 Sep 2019, 23:02 (7 years ago) parentb9e99db
1 file changed+19−4
examples/WordClassifier.hs+19−4
examples/WordClassifier.hs+19−4
@@ -23,19 +23,34 @@ instance (Enum a, Storable a) => Hashable (A.Array a) where
2323 hashWithSalt salt arr = runIdentity $
2424 Streamly.runFold (Streamly.rollingHashWithSalt salt) (A.read arr)
2525
26+{-# INLINE toLower #-}
27+toLower :: Char -> Char
28+toLower c
29+ | uc >= 0x61 && uc <= 0x7a = c
30+ | otherwise = Char.toLower c
31+ where
32+ uc = fromIntegral (Char.ord c) :: Word
33+
34+{-# INLINE isAlpha #-}
35+isAlpha :: Char -> Bool
36+isAlpha c
37+ | uc >= 0x61 && uc <= 0x7a = True
38+ | otherwise = Char.isAlpha c
39+ where
40+ uc = fromIntegral (Char.ord c) :: Word
41+
2642main :: IO ()
2743main =
2844 let
2945 increment m str = Map.insertWith (+) str (1 :: Int) m
30− report = traverse_ print
3146 in do
3247 name <- fmap head getArgs
3348 src <- openFile name ReadMode
3449 FH.read src -- SerialT IO Word8
3550 & Streamly.decodeChar8 -- SerialT IO Char
36− & Streamly.map Char.toLower -- SerialT IO Char
51+ & Streamly.map toLower -- SerialT IO Char
3752 & Streamly.words -- SerialT IO (Array Char)
38− & Streamly.filterM (Streamly.all Char.isAlpha . A.read)
53+ & Streamly.filterM (Streamly.all isAlpha . A.read)
3954 & Streamly.foldl' increment Map.empty
4055 & fmap (List.take 25 . List.sortOn (Ord.Down . snd) . Map.toList)
41− >>= report -- IO ()
56+ >>= traverse_ print