streamly-examples 0.1.3 → 0.2.0
raw patch · 20 files changed
+110/−118 lines, 20 filesdep ~basedep ~conduitdep ~containers
Dependency ranges changed: base, conduit, containers, streamly, streamly-core, tasty-bench, text
Files
- Changelog.md +4/−0
- README.md +31/−34
- examples/CSVParser.hs +3/−4
- examples/CamelCase.hs +4/−4
- examples/CmdServer.hs +7/−7
- examples/ControlFlow.hs +1/−1
- examples/CoreUtils.hs +2/−2
- examples/CoreUtilsHandle.hs +12/−12
- examples/DateTimeParser.hs +6/−6
- examples/EchoServer.hs +3/−3
- examples/FileSender.hs +1/−1
- examples/Intro.hs +3/−6
- examples/MergeServer.hs +4/−4
- examples/MergeSort.hs +4/−5
- examples/Split.hs +1/−1
- examples/WordCountParallel.hs +2/−2
- examples/WordCountParallelUTF8.hs +4/−7
- examples/WordFrequency.hs +3/−6
- examples/WordServer.hs +4/−4
- streamly-examples.cabal +11/−9
Changelog.md view
@@ -1,5 +1,9 @@ # Changelog +## 0.2.0 (Dec 2023)++* Support streamly-0.10.0 and streamly-core-0.2.0+ ## 0.1.3 (Mar 2023) * Support streamly-0.9.0 and streamly-core-0.1.0
README.md view
@@ -43,77 +43,74 @@ ### General -* [Intro](examples/Intro.hs): Simple, introductory examples - loops, text+* [Intro](https://github.com/composewell/streamly-examples/blob/master/examples/Intro.hs): Simple, introductory examples - loops, text processing, networking, concurrency.-* [MergeSort](examples/MergeSort.hs): Sort a stream concurrently using merge+* [MergeSort](https://github.com/composewell/streamly-examples/blob/master/examples/MergeSort.hs): Sort a stream concurrently using merge sort.-* [Rate](examples/Rate.hs): Run an action at a given rate.+* [Rate](https://github.com/composewell/streamly-examples/blob/master/examples/Rate.hs): Run an action at a given rate. ### FileSystem -* [CoreUtils](examples/CoreUtils.hs): Implement simplified coreutils+* [CoreUtils](https://github.com/composewell/streamly-examples/blob/master/examples/CoreUtils.hs): Implement simplified coreutils like `cat`, `cp`, `tee`, `grep` using `Streamly.FileSystem.File` API.-* [CoreUtilsHandle](examples/CoreUtilsHandle.hs): Implement simplified+* [CoreUtilsHandle](https://github.com/composewell/streamly-examples/blob/master/examples/CoreUtilsHandle.hs): Implement simplified coreutils using `Streamly.FileSystem.Handle` API.-* [Split](examples/Split.hs): Implement file splitting utility `split`.-* [FileSystemEvent](examples/FileSystemEvent.hs): File watching/fsnotify API+* [Split](https://github.com/composewell/streamly-examples/blob/master/examples/Split.hs): Implement file splitting utility `split`.+* [FileSystemEvent](https://github.com/composewell/streamly-examples/blob/master/examples/FileSystemEvent.hs): File watching/fsnotify API example.-* [ListDir](examples/ListDir.hs): List a directory tree recursively and+* [ListDir](https://github.com/composewell/streamly-examples/blob/master/examples/ListDir.hs): List a directory tree recursively and concurrently. ### Text Processing -* [CamelCase](examples/CamelCase.hs): Convert a file to camel case.-* [WordCount](examples/WordCount.hs): Simple word counting (`wc`) program.-* [WordCount.c](examples/WordCount.c): C equivalent for perf comparison.-* [WordCountModular](examples/WordCountModular.hs): Modular version.-* [WordCountParallel](examples/WordCountParallel.hs): Concurrent version.-* [WordCountParallelUTF8](examples/WordCountParallelUTF8.hs): Concurrent +* [CamelCase](https://github.com/composewell/streamly-examples/blob/master/examples/CamelCase.hs): Convert a file to camel case.+* [WordCount](https://github.com/composewell/streamly-examples/blob/master/examples/WordCount.hs): Simple word counting (`wc`) program.+* [WordCount.c](https://github.com/composewell/streamly-examples/blob/master/examples/WordCount.c): C equivalent for perf comparison.+* [WordCountModular](https://github.com/composewell/streamly-examples/blob/master/examples/WordCountModular.hs): Modular version.+* [WordCountParallel](https://github.com/composewell/streamly-examples/blob/master/examples/WordCountParallel.hs): Concurrent version.+* [WordCountParallelUTF8](https://github.com/composewell/streamly-examples/blob/master/examples/WordCountParallelUTF8.hs): Concurrent version with full UTF8 handling.-* [WordFrequency](examples/WordFrequency.hs): Count word frequency in+* [WordFrequency](https://github.com/composewell/streamly-examples/blob/master/examples/WordFrequency.hs): Count word frequency in a file and print top 25 words.-* [CSVParser](examples/CSVParser.hs): Process a CSV file+* [CSVParser](https://github.com/composewell/streamly-examples/blob/master/examples/CSVParser.hs): Process a CSV file+* [DateTimeParser](https://github.com/composewell/streamly-examples/blob/master/examples/DateTimeParser.hs): Parse a Date/Time string.+* [LogParser](https://github.com/composewell/streamly-examples/blob/master/examples/LogParser.hs): Parse a quoted string with escaping. ### Networking -* [EchoServer](examples/EchoServer.hs): A concurrent TCP server that+* [EchoServer](https://github.com/composewell/streamly-examples/blob/master/examples/EchoServer.hs): A concurrent TCP server that echoes everything that it receives.-* [MergeServer](examples/MergeServer.hs): Merges lines received from+* [MergeServer](https://github.com/composewell/streamly-examples/blob/master/examples/MergeServer.hs): Merges lines received from several client connections and writes them to a file.-* [FileSender](examples/FileSender.hs): Send many files concurrently to+* [FileSender](https://github.com/composewell/streamly-examples/blob/master/examples/FileSender.hs): Send many files concurrently to a server over multiple connections. Can be used to test `MergeServer`.-* [CmdServer](examples/CmdServer.hs): Receive a stream of commands from many+* [CmdServer](https://github.com/composewell/streamly-examples/blob/master/examples/CmdServer.hs): Receive a stream of commands from many clients and respond to them using command handlers.-* [CmdClient](examples/CmdClient.hs): Run multiple concurrent clients sending+* [CmdClient](https://github.com/composewell/streamly-examples/blob/master/examples/CmdClient.hs): Run multiple concurrent clients sending streams of commands to a server and receiving responses. Can be used to test `CmdServer`.-* [WordServer](examples/WordServer.hs): A word look up (dictionary)+* [WordServer](https://github.com/composewell/streamly-examples/blob/master/examples/WordServer.hs): A word look up (dictionary) server, instead of performing a real DB query the server just adds a time delay to simulate the IO. ### FRP/Games/Animation -* [AcidRain](examples/AcidRain.hs): A console game with deteriorating health+* [AcidRain](https://github.com/composewell/streamly-examples/blob/master/examples/AcidRain.hs): A console game with deteriorating health that can be modified by typing "harm" and "potion" commands.-* [CirclingSquare](examples/CirclingSquare.hs): Use SDL2 to display a+* [CirclingSquare](https://github.com/composewell/streamly-examples/blob/master/examples/CirclingSquare.hs): Use SDL2 to display a square that moves in a circle, and follows the mouse pointer. -### Monad Transformers--* [ControlFlow](examples/ControlFlow.hs): Combining control flow manipulating- transformers (`MaybeT`, `ExceptT`, `ContT`) with streamly.- ### Interoperation -* [Interop.Streaming](examples/Interop/Streaming.hs): Converting streamly+* [Interop.Streaming](https://github.com/composewell/streamly-examples/blob/master/examples/Interop/Streaming.hs): Converting streamly stream type to and from `streaming` stream type.-* [Interop.Pipes](examples/Interop/Pipes.hs): Converting streamly+* [Interop.Pipes](https://github.com/composewell/streamly-examples/blob/master/examples/Interop/Pipes.hs): Converting streamly stream type to and from `pipes` stream type.-* [Interop.Conduit](examples/Interop/Conduit.hs): Converting streamly+* [Interop.Conduit](https://github.com/composewell/streamly-examples/blob/master/examples/Interop/Conduit.hs): Converting streamly stream type to and from `conduit` stream type.-* [Interop.Vector](examples/Interop/Vector.hs): Converting streamly+* [Interop.Vector](https://github.com/composewell/streamly-examples/blob/master/examples/Interop/Vector.hs): Converting streamly stream type to and from `vector` stream type. ## Licensing -Available under [Apache-2.0 license](LICENSE).+Available under [Apache-2.0 license](https://github.com/composewell/streamly-examples/blob/master/LICENSE).
examples/CSVParser.hs view
@@ -13,15 +13,14 @@ import qualified Streamly.Data.Stream as Stream import qualified Streamly.FileSystem.Handle as Handle import qualified System.IO as IO-import qualified Streamly.Internal.Data.Stream as Stream (splitOn)-import qualified Streamly.Internal.Data.Stream.Chunked as ArrayStream (splitOn)+import qualified Streamly.Internal.Data.Array.Stream as ArrayStream (splitOn) main :: IO () main = do inFile <- fmap head getArgs src <- IO.openFile inFile ReadMode - Stream.unfold Handle.chunkReader src -- Stream IO (Array Word8)+ Handle.readChunks src -- Stream IO (Array Word8) & ArrayStream.splitOn 10 -- Stream IO (Array Word8) & Stream.fold (Fold.drainMapM parseLine) -- IO () @@ -30,7 +29,7 @@ printList = putStr . map (chr . fromIntegral) parseLine arr =- (Stream.unfold Array.reader arr :: Stream IO Word8)+ (Array.read arr :: Stream IO Word8) & Stream.splitOn (== 44) Fold.toList -- Stream IO [Word8] & Stream.intersperse [32] -- Stream IO [Word8] & Stream.fold (Fold.drainMapM printList) -- IO ()
examples/CamelCase.hs view
@@ -13,10 +13,10 @@ -- | @camelCase source-file dest-file@ camelCase :: Handle -> Handle -> IO () camelCase src dst =- Stream.unfold Handle.reader src -- Stream IO Word8- & Stream.postscan fold -- Stream IO (Bool, Maybe Word8)- & Stream.mapMaybe snd -- Stream IO Word8- & Stream.fold (Handle.write dst) -- IO ()+ Handle.read src -- Stream IO Word8+ & Stream.postscan fold -- Stream IO (Bool, Maybe Word8)+ & Stream.mapMaybe snd -- Stream IO Word8+ & Stream.fold (Handle.write dst) -- IO () where
examples/CmdServer.hs view
@@ -75,13 +75,13 @@ handler :: Socket -> IO () handler sk =- Stream.unfold Socket.reader sk -- Stream IO Word8- & Unicode.decodeLatin1 -- Stream IO Char- & Stream.parseMany word -- Stream IO String+ Socket.read sk -- Stream IO Word8+ & Unicode.decodeLatin1 -- Stream IO Char+ & Stream.parseMany word -- Stream IO String & Stream.catRights- & fmap (, sk) -- Stream IO (String, Socket)- & Stream.fold demux -- IO () + Exceptions- & discard -- IO ()+ & fmap (, sk) -- Stream IO (String, Socket)+ & Stream.fold demux -- IO () + Exceptions+ & discard -- IO () where @@ -94,7 +94,7 @@ server :: IO () server =- Stream.unfold TCP.acceptorOnPort 8091 -- Stream IO Socket+ TCP.accept 8091 -- Stream IO Socket & Stream.parMapM id (Socket.forSocketM handler) -- Stream IO () & Stream.fold Fold.drain -- IO ()
examples/ControlFlow.hs view
@@ -27,7 +27,7 @@ import Control.Monad.Trans.Except (ExceptT, runExceptT, throwE, catchE) import Control.Monad.Trans.Cont (ContT(..), callCC) import Streamly.Data.Stream (Stream)-import Streamly.Internal.Data.Stream.StreamD (CrossStream, mkCross, unCross)+import Streamly.Internal.Data.Stream (CrossStream, mkCross, unCross) import qualified Streamly.Data.Fold as Fold import qualified Streamly.Data.Stream as Stream
examples/CoreUtils.hs view
@@ -38,7 +38,7 @@ append :: IO () append = File.readChunks "input.txt" -- Stream IO (Array Word8)- & File.appendChunks "output.txt" -- IO ()+ & File.writeAppendChunks "output.txt" -- IO () -- | > cat input.txt | tee output1.txt > output.txt tap :: IO ()@@ -51,7 +51,7 @@ -- output1.txt is processed/written to in a separate thread. tapAsync :: IO () tapAsync =- Stream.unfold Stdio.readChunks () -- Stream IO (Array Word8)+ Stdio.readChunks () -- Stream IO (Array Word8) & Stream.tapAsyncK (File.fromChunks "output1.txt") -- Stream IO (Array Word8) & File.fromChunks "output.txt" -- IO ()
examples/CoreUtilsHandle.hs view
@@ -10,7 +10,7 @@ import qualified Streamly.FileSystem.Handle as Handle import qualified Streamly.Unicode.Stream as Unicode -import qualified Streamly.Internal.Data.Stream.Chunked as ArrayStream (splitOn)+import qualified Streamly.Internal.Data.Array.Stream as ArrayStream (splitOn) -- | Read the contents of a file to stdout. --@@ -20,20 +20,20 @@ -- catBytes :: Handle -> IO () catBytes src =- Stream.unfold Handle.reader src -- Stream IO Word8+ Handle.read src -- Stream IO Word8 & Stream.fold (Handle.write stdout) -- IO () -- | Chunked version, more efficient than the byte stream version above. Reads -- the file in 256KB chunks and writes those chunks to stdout. cat :: Handle -> IO () cat src =- Stream.unfold Handle.chunkReaderWith (256*1024, src) -- Stream IO (Array Word8)+ Handle.readChunksWith (256*1024) src -- Stream IO (Array Word8) & Stream.fold (Handle.writeChunks stdout) -- IO () -- | Read from standard input write to standard output echo :: IO () echo =- Stream.unfold Handle.chunkReader stdin -- Stream IO (Array Word8)+ Handle.readChunks stdin -- Stream IO (Array Word8) & Stream.fold (Handle.writeChunks stdout) -- IO () -- | Copy a source file to a destination file.@@ -43,7 +43,7 @@ -- 32KB and writes those chunks to the destination file. cpBytes :: Handle -> Handle -> IO () cpBytes src dst =- Stream.unfold Handle.reader src -- Stream IO Word8+ Handle.read src -- Stream IO Word8 & Stream.fold (Handle.write dst) -- IO () -- | Chunked version, more efficient than the byte stream version above. Reads@@ -51,7 +51,7 @@ cp :: Handle -> Handle -> IO () cp src dst = Stream.fold (Handle.writeChunks dst)- $ Stream.unfold Handle.chunkReaderWith (256*1024, src)+ $ Handle.readChunksWith (256*1024) src -- | Count lines like wc -l. --@@ -59,9 +59,9 @@ -- and counts the lines.. wclChar :: Handle -> IO Int wclChar src =- Stream.unfold Handle.reader src -- Stream IO Word8- & Unicode.decodeLatin1 -- Stream IO Char- & split (== '\n') Fold.drain -- Stream IO ()+ Handle.read src -- Stream IO Word8+ & Unicode.decodeLatin1 -- Stream IO Char+ & split (== '\n') Fold.drain -- Stream IO () & Stream.fold Fold.length where@@ -73,9 +73,9 @@ -- first. wcl :: Handle -> IO Int wcl src =- Stream.unfold Handle.chunkReader src -- Stream IO (Array Word8)- & ArrayStream.splitOn 10 -- Stream IO (Array Word8)- & Stream.fold Fold.length -- IO ()+ Handle.readChunks src -- Stream IO (Array Word8)+ & ArrayStream.splitOn 10 -- Stream IO (Array Word8)+ & Stream.fold Fold.length -- IO () main :: IO () main = do
examples/DateTimeParser.hs view
@@ -47,7 +47,7 @@ {-# INLINE foldDateTime #-} foldDateTime :: Array Char -> IO (Int, Int) foldDateTime arr =- Stream.fold t $ Stream.unfold Array.reader arr+ Stream.fold t $ Array.read arr where @@ -121,7 +121,7 @@ <* char ':' <*> decimal 2 -- sec <* char 'Z'- in Stream.fold t $ Stream.unfold Array.reader arr+ in Stream.fold t $ Array.read arr ------------------------------------------------------------------------------- -- Using foldBreak - slower than applicative@@ -130,7 +130,7 @@ {-# INLINE _foldBreakDateTime #-} _foldBreakDateTime :: Array Char -> IO Int _foldBreakDateTime arr = do- let s = Stream.unfold Array.reader arr+ let s = Array.read arr (year, s1) <- Stream.foldBreak (decimal 4) s (_, s2) <- Stream.foldBreak (char '-') s1 (month, s3) <- Stream.foldBreak (decimal 2) s2@@ -153,7 +153,7 @@ _parseBreakDateTime :: Array Char -> IO Int _parseBreakDateTime arr = do let s = StreamK.fromStream $ Stream.fromPure arr- p = ParserK.fromParser . Parser.fromFold+ p = ParserK.adaptC . Parser.fromFold (Right year, s1) <- StreamK.parseBreakChunks (p $ decimal 4) s (_, s2) <- StreamK.parseBreakChunks (p $ char '-') s1 (Right month, s3) <- StreamK.parseBreakChunks (p $ decimal 2) s2@@ -180,7 +180,7 @@ where - p = ParserK.fromParser+ p = ParserK.adaptC dateParser = do year <- p $ Parser.decimal <* Parser.char '-'@@ -196,7 +196,7 @@ {-# NOINLINE _parseDateTime #-} _parseDateTime :: Array Char -> IO Int _parseDateTime arr = do- r <- Stream.parse dateParser $ Stream.unfold Array.reader arr+ r <- Stream.parse dateParser $ Array.read arr return $ fromRight (error "failed") r where
examples/EchoServer.hs view
@@ -12,7 +12,7 @@ main :: IO () main =- Stream.unfold TCP.acceptorOnPort 8091 -- Stream IO Socket+ TCP.accept 8091 -- Stream IO Socket & Stream.parMapM id (handleWithM echo) -- Stream IO () & Stream.fold Fold.drain -- IO () @@ -20,8 +20,8 @@ echo :: Socket -> IO () echo sk =- Stream.unfold Socket.chunkReaderWith (32768, sk) -- Stream IO (Array Word8)- & Stream.fold (Socket.writeChunks sk) -- IO ()+ Socket.readChunksWith 32768 sk -- Stream IO (Array Word8)+ & Stream.fold (Socket.writeChunks sk) -- IO () handleWithM :: (Socket -> IO ()) -> Socket -> IO () handleWithM f sk = finally (f sk) (Net.close sk)
examples/FileSender.hs view
@@ -30,7 +30,7 @@ fileToSocket :: Handle -> Socket -> IO () fileToSocket fh sk =- Stream.unfold Handle.chunkReader fh -- Stream IO (Array Word8)+ Handle.readChunks fh -- Stream IO (Array Word8) & Stream.fold (Socket.writeChunks sk) -- IO () sendFile :: String -> IO ()
examples/Intro.hs view
@@ -21,9 +21,6 @@ import qualified Streamly.FileSystem.File as File import qualified Streamly.Unicode.Stream as Unicode -import qualified Streamly.Internal.Data.Stream as Stream (wordsBy)-import qualified Streamly.Internal.Data.Unfold as Unfold (enumerateFromToIntegral)- ------------------------------------------------------------------------------- -- Simple loops -------------------------------------------------------------------------------@@ -31,7 +28,7 @@ -- | Sum a list of Int sumInt :: Identity Int sumInt =- Stream.unfold Unfold.fromList [1..10] -- Stream Identity Int+ Stream.fromList [1..10] -- Stream Identity Int & Stream.fold Fold.sum -- Identity Int -- | Sum a list of Int@@ -56,8 +53,8 @@ xmult :: Unfold Identity ((Int, Int), (Int, Int)) Int xmult = Unfold.crossWith (*)- (Unfold.lmap fst Unfold.enumerateFromToIntegral)- (Unfold.lmap snd Unfold.enumerateFromToIntegral)+ (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
examples/MergeServer.hs view
@@ -17,9 +17,9 @@ -- a limit to the buffering for safety. readLines :: Socket -> Stream IO (Array Char) readLines sk =- Stream.unfold Socket.reader sk -- Stream IO Word8- & Unicode.decodeLatin1 -- Stream IO Char- & split (== '\n') Array.write -- Stream IO String+ Socket.read sk -- Stream IO Word8+ & Unicode.decodeLatin1 -- Stream IO Char+ & split (== '\n') Array.write -- Stream IO String where @@ -34,7 +34,7 @@ -- streams at line boundaries and writes the merged stream to a file. server :: Handle -> IO () server file =- Stream.unfold TCP.acceptorOnPort 8090 -- Stream IO Socket+ TCP.accept 8090 -- Stream IO Socket & Stream.parConcatMap (Stream.eager True) recv -- Stream IO (Array Char) & Stream.unfoldMany Array.reader -- Stream IO Char & Unicode.encodeLatin1 -- Stream IO Word8
examples/MergeSort.hs view
@@ -28,7 +28,7 @@ K.toStream . K.sortBy compare . K.fromStream- . Stream.unfold Array.reader+ . Array.read sortChunk :: Array Int -> IO (Array Int) sortChunk = Stream.fold Array.write . streamChunk@@ -63,8 +63,7 @@ & Stream.chunksOf chunkSize & f sortChunk & K.fromStream- & K.mergeMapWith- (K.mergeBy compare) (K.fromStream . Stream.unfold Array.reader)+ & K.mergeMapWith (K.mergeBy compare) (K.fromStream . Array.read) & K.toStream & Stream.fold Fold.drain @@ -77,8 +76,8 @@ reduce arr1 arr2 = Stream.mergeBy compare- (Stream.unfold Array.reader arr1)- (Stream.unfold Array.reader arr2)+ (Array.read arr1)+ (Array.read arr2) & Stream.fold Array.write sortMergeChunks ::
examples/Split.hs view
@@ -29,7 +29,7 @@ -- of directory names. splitFile :: Handle -> IO () splitFile inHandle =- (Stream.unfold Handle.reader inHandle :: Stream IO Word8) -- Stream IO Word8+ (Handle.read inHandle :: Stream IO Word8) -- Stream IO Word8 & Stream.liftInner -- Stream (StateT (Maybe (Handle, Int)) IO) Word8 -- Stream (StateT (Maybe (Handle, Int)) IO) () & Stream.refoldMany (Refold.take (180 * mb) Handle.writer) newHandle
examples/WordCountParallel.hs view
@@ -19,8 +19,8 @@ -- Get the line, word, char counts in one chunk. countArray :: Array Word8 -> IO Counts countArray arr =- Stream.unfold Array.reader arr -- Stream IO Word8- & Stream.decodeLatin1 -- Stream IO Char+ Array.read arr -- Stream IO Word8+ & Stream.decodeLatin1 -- Stream IO Char & Stream.fold (Fold.foldl' count (Counts 0 0 0 True)) -- IO Counts -- When combining the counts in two contiguous chunks, we would also need to
examples/WordCountParallelUTF8.hs view
@@ -49,10 +49,7 @@ import qualified Streamly.Internal.Unicode.Stream as Unicode (DecodeState, DecodeError(..), CodePoint, decodeUtf8Either , resumeDecodeUtf8Either)-import qualified Streamly.Internal.Data.Array.Mut.Type as MArray- (getIndexUnsafe, putIndexUnsafe, modifyIndexUnsafe) - ------------------------------------------------------------------------------- -- Parallel char, line and word counting -------------------------------------------------------------------------------@@ -195,7 +192,7 @@ newCounts :: IO (MutArray Int) newCounts = do- counts <- MArray.newPinned (fromEnum (maxBound :: Field) + 1)+ counts <- MArray.pinnedNew (fromEnum (maxBound :: Field) + 1) writeField counts LineCount 0 writeField counts WordCount 0 writeField counts CharCount 0@@ -566,7 +563,7 @@ counts <- newCounts Stream.fold (Fold.drainMapM (countChar counts)) $ Unicode.decodeUtf8Either- $ Stream.unfold Array.reader src+ $ Array.read src return counts {-# INLINE wcMwlParallel #-}@@ -578,7 +575,7 @@ . Stream.ordered True ) countArray- $ Stream.unfold Handle.chunkReaderWith (n, src)+ $ Handle.readChunksWith n src ------------------------------------------------------------------------------- -- Serial counting using parallel version of countChar@@ -590,7 +587,7 @@ counts <- newCounts Stream.fold (Fold.drainMapM (countChar counts)) $ Unicode.decodeUtf8Either- $ Stream.unfold Handle.reader src+ $ Handle.read src return counts -------------------------------------------------------------------------------
examples/WordFrequency.hs view
@@ -9,24 +9,21 @@ import Data.Hashable (Hashable(..)) import System.Environment (getArgs) import Streamly.Data.Array (Unbox)-import Streamly.Internal.Data.IsMap.HashMap () import qualified Data.Char as Char import qualified Data.HashMap.Strict as Map import qualified Data.List as List import qualified Data.Ord as Ord import qualified Streamly.Data.Array as Array-import qualified Streamly.Data.Fold as Fold-import qualified Streamly.Internal.Data.Fold.Container as Fold (toContainerIO)+import qualified Streamly.Data.Fold.Prelude as Fold import qualified Streamly.Data.Stream as Stream-import qualified Streamly.Internal.Data.Stream as Stream (wordsBy) import qualified Streamly.FileSystem.File as File import qualified Streamly.Unicode.Stream as Unicode hashArray :: (Unbox a, Integral b, Num c) => Fold.Fold Identity a b -> Array.Array a -> c hashArray f arr =- Stream.unfold Array.reader arr+ Array.read arr & Stream.fold f & fromIntegral . runIdentity @@ -61,7 +58,7 @@ inFile <- fmap head getArgs let counter = Fold.foldl' (\n _ -> n + 1) (0 :: Int)- classifier = Fold.toContainerIO id counter+ classifier = Fold.toHashMapIO id counter -- Write the stream to a hashmap consisting of word counts mp <- File.read inFile -- Stream IO Word8
examples/WordServer.hs view
@@ -21,7 +21,7 @@ -- connection is closed. lookupWords :: Socket -> IO () lookupWords sk =- Stream.unfold Socket.reader sk -- Stream IO Word8+ Socket.read sk -- Stream IO Word8 & Unicode.decodeLatin1 -- Stream IO Char & Stream.parseMany word & Stream.catRights -- Stream IO String@@ -43,6 +43,6 @@ -- "nc" as a client to try it out. main :: IO () main =- Stream.unfold TCP.acceptorOnPort 8091 -- Stream IO Socket- & Stream.parMapM id (Socket.forSocketM serve) -- Stream IO ()- & Stream.fold Fold.drain -- IO ()+ TCP.accept 8091 -- Stream IO Socket+ & Stream.parMapM id (Socket.forSocketM serve) -- Stream IO ()+ & Stream.fold Fold.drain -- IO ()
streamly-examples.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: streamly-examples-version: 0.1.3+version: 0.2.0 license: Apache-2.0 license-file: LICENSE author: Composewell Technologies@@ -60,12 +60,12 @@ common exe-dependencies build-depends:- streamly == 0.9.0- , streamly-core == 0.1.0- , base >= 4.9 && < 4.18+ streamly == 0.10.0+ , streamly-core == 0.2.0+ , base >= 4.9 && < 4.20 , directory >= 1.2 && < 1.4 , transformers >= 0.4 && < 0.7- , containers >= 0.5 && < 0.7+ , containers >= 0.5 && < 0.8 , random >= 1.0.0 && < 2 , exceptions >= 0.8 && < 0.11 , transformers-base >= 0.4 && < 0.5@@ -93,6 +93,8 @@ -Wincomplete-uni-patterns -Wredundant-constraints -Wnoncanonical-monad-instances+ if impl(ghc >= 9.8)+ ghc-options: -Wno-x-partial if flag(fusion-plugin) && !impl(ghcjs) && !impl(ghc < 8.6) ghc-options: -fplugin Fusion.Plugin@@ -139,7 +141,7 @@ main-is: Interop/Conduit.hs if !impl(ghcjs) && flag(interop) buildable: True- build-depends: conduit >= 1.3.4.1 && < 1.3.5+ build-depends: conduit >= 1.3.4.1 && < 1.4 else buildable: False @@ -209,7 +211,7 @@ if flag(sdl2) buildable: True build-Depends:- text >= 1.2.3 && < 1.3+ text >= 1.2.3 && < 2.2 , sdl2 >= 2.5.0 && < 2.6 if os(darwin) frameworks: Cocoa@@ -331,7 +333,7 @@ main-is: DateTimeParser.hs if !impl(ghcjs) buildable: True- build-depends: tasty-bench >= 0.3+ build-depends: tasty-bench >= 0.3 && < 0.4 else buildable: False @@ -340,6 +342,6 @@ main-is: LogParser.hs if !impl(ghcjs) buildable: True- build-depends: tasty-bench >= 0.3+ build-depends: tasty-bench >= 0.3 && < 0.4 else buildable: False