@@ -124,6 +124,46 @@ benchmarks](https://github.com/composewell/streaming-benchmarks).
124124
125125
126126
127+## File IO
128+
129+The following code snippet implements some common Unix command line utilities
130+using streamly. To get an idea about IO streaming performance, you can
131+benchmark these against the regular unix utilities using the `time` command.
132+Make 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.
136+
137+``` haskell
138+import qualified Streamly.Prelude as S
139+import qualified Streamly.Fold as FL
140+import qualified Streamly.Mem.Array as A
141+import qualified Streamly.FileSystem.File as File
142+
143+import Data.Char (ord)
144+import System.Environment (getArgs)
145+import System.IO (openFile, IOMode(..), stdout)
146+
147+cat src = File.writeArrays stdout $ File.readArrays src
148+cp src dst = File.writeArrays dst $ File.readArrays src
149+wcl src = print =<< (S.length
150+ $ FL.splitSuffixBy (== fromIntegral (ord '\n')) FL.drain
151+ $ File.read src)
152+grepc pat src = print . (subtract 1) =<< (S.length
153+ $ FL.splitOn (A.fromList (map (fromIntegral . ord) pat)) FL.drain
154+ $ File.read src)
155+
156+main = do
157+ name <- fmap head getArgs
158+ src <- openFile name ReadMode
159+ -- cat src -- Unix cat program
160+ -- wcl src -- Unix wc -l program
161+ grepc "aaaa" src -- Unix grep -c program
162+
163+ -- dst <- openFile "dst.txt" WriteMode
164+ -- cp src dst -- Unix cp program
165+```
166+
127167## Streaming Pipelines
128168
129169The following snippet provides a simple stream composition example that reads