on master help sign in
update file IO example
b9e7999Harendra Kumar
README.md+27−6
@@ -130,9 +130,12 @@ The following code snippet implements some common Unix command line utilities
130using streamly. To get an idea about IO streaming performance, you can
130using streamly. To get an idea about IO streaming performance, you can
131benchmark these against the regular unix utilities using the `time` command.
131benchmark these against the regular unix utilities using the `time` command.
132Make sure to use a big enough input file and compile with `ghc -O2` when
132Make sure to use a big enough input file and compile with `ghc -O2` when
133+benchmarking. Use `+RTS -s` flags on the executable to check the space usage,
134+look for `maximum residency` in the output.
135+
133−benchmarking. Note that `grep -c` counts the number of lines where the pattern
136+Note that `grep -c` counts the number of lines where the pattern occurs whereas
134−occurs whereas the snippet below counts the total number of occurrences of the
137+the snippet below counts the total number of occurrences of the pattern,
135−pattern, therefore, the output may differ.
138+therefore, the output may differ.
136
139
137``` haskell
140``` haskell
138import qualified Streamly.Prelude as S
141import qualified Streamly.Prelude as S
@@ -146,22 +149,40 @@ import System.IO (openFile, IOMode(..), stdout)
146
149
147cat src = File.writeArrays stdout $ File.readArrays src
150cat src = File.writeArrays stdout $ File.readArrays src
148cp src dst = File.writeArrays dst $ File.readArrays src
151cp src dst = File.writeArrays dst $ File.readArrays src
149−wcl src = print =<< (S.length
152+wcl src = print =<<
153+ ( S.length
150 $ FL.splitSuffixBy (== fromIntegral (ord '\n')) FL.drain
154 $ FL.splitSuffixBy (== fromIntegral (ord '\n')) FL.drain
151 $ File.read src)
155 $ File.read src)
152−grepc pat src = print . (subtract 1) =<< (S.length
156+grepc pat src = print . (subtract 1) =<<
157+ ( S.length
153 $ FL.splitOn (A.fromList (map (fromIntegral . ord) pat)) FL.drain
158 $ FL.splitOn (A.fromList (map (fromIntegral . ord) pat)) FL.drain
154 $ File.read src)
159 $ File.read src)
160+avgll src = print =<<
161+ ( FL.foldl' avg
162+ $ FL.splitSuffixBy (== fromIntegral (ord '\n')) FL.length
163+ $ File.read src)
164+ where avg = (/) <$> toDouble FL.sum <*> toDouble FL.length
165+ toDouble = fmap (fromIntegral :: Int -> Double)
166+llhisto src = print =<<
167+ ( FL.foldl' (FL.classify FL.length)
168+ $ S.map bucket
169+ $ FL.splitSuffixBy (== fromIntegral (ord '\n')) FL.length
170+ $ File.read src)
171+ where
172+ bucket n = let i = n `div` 10 in if i > 9 then (9,n) else (i,n)
155
173
156main = do
174main = do
157 name <- fmap head getArgs
175 name <- fmap head getArgs
158 src <- openFile name ReadMode
176 src <- openFile name ReadMode
159 -- cat src -- Unix cat program
177 -- cat src -- Unix cat program
160 -- wcl src -- Unix wc -l program
178 -- wcl src -- Unix wc -l program
161− grepc "aaaa" src -- Unix grep -c program
179+ -- grepc "aaaa" src -- Unix grep -c program
162
180
163 -- dst <- openFile "dst.txt" WriteMode
181 -- dst <- openFile "dst.txt" WriteMode
164 -- cp src dst -- Unix cp program
182 -- cp src dst -- Unix cp program
183+
184+ -- avgll src -- get average line length
185+ llhisto src -- get line length histogram
165```
186```
166
187
167## Streaming Pipelines
188## Streaming Pipelines