@@ -130,9 +130,12 @@ The following code snippet implements some common Unix command line utilities
130130using streamly. To get an idea about IO streaming performance, you can
131131benchmark these against the regular unix utilities using the `time` command.
132132Make sure to use a big enough input file and compile with `ghc -O2` when
133−benchmarking. Note that `grep -c` counts the number of lines where the pattern
134−occurs whereas the snippet below counts the total number of occurrences of the
135−pattern, therefore, the output may differ.
133+benchmarking. Use `+RTS -s` flags on the executable to check the space usage,
134+look for `maximum residency` in the output.
135+
136+Note that `grep -c` counts the number of lines where the pattern occurs whereas
137+the snippet below counts the total number of occurrences of the pattern,
138+therefore, the output may differ.
136139
137140``` haskell
138141import qualified Streamly.Prelude as S
@@ -146,22 +149,40 @@ import System.IO (openFile, IOMode(..), stdout)
146149
147150cat src = File.writeArrays stdout $ File.readArrays src
148151cp src dst = File.writeArrays dst $ File.readArrays src
149−wcl src = print =<< (S.length
152+wcl src = print =<<
153+ ( S.length
150154 $ FL.splitSuffixBy (== fromIntegral (ord '\n')) FL.drain
151155 $ File.read src)
152−grepc pat src = print . (subtract 1) =<< (S.length
156+grepc pat src = print . (subtract 1) =<<
157+ ( S.length
153158 $ FL.splitOn (A.fromList (map (fromIntegral . ord) pat)) FL.drain
154159 $ 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)
155173
156174main = do
157175 name <- fmap head getArgs
158176 src <- openFile name ReadMode
159177 -- cat src -- Unix cat program
160178 -- wcl src -- Unix wc -l program
161− grepc "aaaa" src -- Unix grep -c program
179+ -- grepc "aaaa" src -- Unix grep -c program
162180
163181 -- dst <- openFile "dst.txt" WriteMode
164182 -- cp src dst -- Unix cp program
183+
184+ -- avgll src -- get average line length
185+ llhisto src -- get line length histogram
165186```
166187
167188## Streaming Pipelines