-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIntro.hs
200 lines (167 loc) · 7.16 KB
/
Intro.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
-- Introductory example programs
import Control.Concurrent (threadDelay)
import Data.Char (ord, isSpace)
import Data.Functor.Identity (Identity(..))
import Data.Function ((&))
import Data.Map.Strict (Map)
import Data.Word (Word8)
import System.Environment (getArgs)
import System.IO (stdout)
import Streamly.Data.Fold (Fold, Tee(..))
import Streamly.Data.Stream.Prelude (Stream)
import Streamly.Data.Unfold (Unfold)
import Streamly.Internal.Data.Stream (CrossStream, mkCross, unCross)
import qualified Streamly.Data.Array as Array
import qualified Streamly.Data.Fold as Fold
import qualified Streamly.Data.Stream.Prelude as Stream
import qualified Streamly.Data.Unfold as Unfold
import qualified Streamly.FileSystem.Handle as Handle
import qualified Streamly.FileSystem.File as File
import qualified Streamly.Unicode.Stream as Unicode
-------------------------------------------------------------------------------
-- Simple loops
-------------------------------------------------------------------------------
-- | Sum a list of Int
sumInt :: Identity Int
sumInt =
Stream.fromList [1..10] -- Stream Identity Int
& Stream.fold Fold.sum -- Identity Int
-- | Sum a list of Int
sumInt1 :: Identity Int
sumInt1 =
Stream.fromList [1..10] -- Stream Identity Int
& Stream.fold Fold.sum -- Identity Int
-------------------------------------------------------------------------------
-- Nested loops
-------------------------------------------------------------------------------
-- | Nested looping example using unfolds. This is the most efficient way to do
-- nested loops (or cross product) when the two streams do not depend on each
-- other. The loops fuse completely generating code equivalent to C.
crossProduct :: (Int,Int) -> (Int,Int) -> Identity Int
crossProduct range1 range2 =
let
-- cross multiply src1 and src2 e.g.
-- if src1 = [1,2], and src2 = [3,4] then src1 x src2 =
-- [(1*3),(1*4),(2*3),(2*4)]
xmult :: Unfold Identity ((Int, Int), (Int, Int)) Int
xmult =
Unfold.crossWith (*)
(Unfold.lmap fst Unfold.enumerateFromTo)
(Unfold.lmap snd Unfold.enumerateFromTo)
in Stream.unfold xmult (range1,range2) -- Stream Identity Int
& Stream.fold Fold.sum -- Identity Int
-- | Nested looping similar to 'cross' above but more general and less
-- efficient. The second stream may depend on the first stream. The loops
-- cannot fuse completely.
--
nestedLoops :: CrossStream IO ()
nestedLoops = do
x <- mkCross $ Stream.fromList [3,4 :: Int]
y <- mkCross $ Stream.fromList [1..x]
mkCross $ Stream.fromEffect $ print (x, y)
-------------------------------------------------------------------------------
-- Text processing
-------------------------------------------------------------------------------
splitOn :: Monad m => (a -> Bool) -> Fold m a b -> Stream m a -> Stream m b
splitOn p f = Stream.foldMany (Fold.takeEndBy_ p f)
-- | Find average line length for lines in a text file
avgLineLength :: IO Double
avgLineLength =
File.read "input.txt" -- Stream IO Word8
& splitOn isNewLine Fold.length -- Stream IO Int
& Stream.fold avg -- IO Double
where
isNewLine :: Word8 -> Bool
isNewLine = (== (fromIntegral . ord) '\n')
toDouble :: Fold IO Int Int -> Fold IO Int Double
toDouble = fmap (fromIntegral :: Int -> Double)
avg :: Fold IO Int Double
avg = unTee $ (/)
<$> Tee (toDouble Fold.sum)
<*> Tee (toDouble Fold.length)
{-# INLINE kvMap #-}
kvMap :: (Monad m, Ord k) => Fold m a b -> Fold m (k, a) (Map k b)
kvMap = Fold.toMap fst . Fold.lmap snd
-- | Read text from a file and generate a histogram of line length
lineLengthHistogram :: IO (Map Int Int)
lineLengthHistogram =
File.read "input.txt" -- Stream IO Word8
& splitOn isNewLine Fold.length -- Stream IO Int
& fmap bucket -- Stream IO (Int, Int)
& Stream.fold (kvMap Fold.length) -- IO (Map Int Int)
where
isNewLine :: Word8 -> Bool
isNewLine = (== (fromIntegral . ord) '\n')
bucket :: Int -> (Int, Int)
bucket n = let i = n `mod` 10 in if i > 9 then (9,n) else (i,n)
-- | Read text from a file and generate a histogram of word length
wordLengthHistogram :: IO (Map Int Int)
wordLengthHistogram =
File.read "input.txt" -- Stream IO Word8
& Unicode.decodeLatin1 -- Stream IO Char
& Stream.wordsBy isSpace Fold.length -- Stream IO Int
& fmap bucket -- Stream IO (Int, Int)
& Stream.fold (kvMap Fold.length) -- IO (Map (Int, Int))
where
bucket :: Int -> (Int, Int)
bucket n = let i = n `mod` 10 in if i > 9 then (9,n) else (i,n)
-------------------------------------------------------------------------------
-- Network/Concurrency
-------------------------------------------------------------------------------
-- Simulate network/db query by adding a delay
fetch :: String -> IO (String, String)
fetch w = threadDelay 1000000 >> return (w,w)
wordList :: [String]
wordList = ["cat", "dog", "mouse"]
meanings :: [IO (String, String)]
meanings = map fetch wordList
-- | Fetch word meanings for words in 'wordList'. All searches are performed
-- concurrently.
--
getWords :: IO ()
getWords =
Stream.fromList meanings -- Stream IO (IO (String, String))
& Stream.parSequence
(Stream.ordered True) -- Stream IO (String, String)
& fmap show -- Stream IO String
& unlinesBy "\n" -- Stream IO String
& fmap Array.fromList -- Stream IO (Array Word8)
& Stream.fold (Handle.writeChunks stdout) -- IO ()
where unlinesBy = Stream.intercalateSuffix (Unfold.function id)
-------------------------------------------------------------------------------
-- Main
-------------------------------------------------------------------------------
-- Usage example: Intro sumInt
--
-- Some examples (e.g. avgLineLength) require a text file "input.txt" in
-- the current directory.
--
main :: IO ()
main = do
cmd <- fmap head getArgs
case cmd of
"sumInt" -> do
putStrLn "sumInt"
print (runIdentity sumInt)
"sumInt1" -> do
putStrLn "sumInt1"
print (runIdentity sumInt1)
"crossProduct" -> do
putStrLn "crossProduct"
print $ runIdentity $ crossProduct (1,1000) (1000,2000)
"nestedLoops" -> do
putStrLn "nestedLoops"
Stream.fold Fold.drain $ unCross nestedLoops
"avgLineLength" -> do
putStrLn "avgLineLength"
avgLineLength >>= print
"lineLengthHistogram" -> do
putStrLn "lineLengthHistogram"
lineLengthHistogram >>= print
"wordLengthHistogram" -> do
putStrLn "wordLengthHistogram"
wordLengthHistogram >>= print
"getWords" -> do
putStrLn "getWords"
getWords >>= print
_ -> putStrLn $ "Unknown command: " ++ cmd