streaming-bytestring: Fast, effectful byte streams.

[ bsd3, data, library, pipes, streaming ] [ Propose Tags ]

This library enables fast and safe streaming of byte data, in either Word8 or Char form. It is a core addition to the streaming ecosystem and avoids the usual pitfalls of combinbing lazy ByteStrings with lazy IO.

We follow the philosophy shared by streaming that "the best API is the one you already know". Thus this library mirrors the API of the bytestring library as closely as possible.

See the module documentation and the README for more information.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.0.3, 0.1.0.4, 0.1.0.5, 0.1.0.6, 0.1.0.7, 0.1.0.8, 0.1.1.0, 0.1.2.0, 0.1.2.2, 0.1.3.0, 0.1.4.0, 0.1.4.2, 0.1.4.3, 0.1.4.4, 0.1.4.5, 0.1.4.6, 0.1.5, 0.1.6, 0.1.7, 0.2.0, 0.2.1, 0.2.2, 0.2.3, 0.2.4, 0.3.0, 0.3.1, 0.3.2, 0.3.3 (info)
Change log CHANGELOG.md
Dependencies base (>=4.9 && <5.0), bytestring (>=0.10.4 && <0.13), deepseq (>=1.4 && <1.6), exceptions (>=0.8 && <0.11), ghc-prim (>=0.4 && <0.12), mmorph (>=1.0 && <1.3), mtl (>=2.2 && <2.4), resourcet (>=1.1 && <1.4), semigroups (>=0.18 && <0.19), streaming (>=0.1.4.0 && <0.3), transformers (>=0.4 && <0.7), transformers-base (>=0.4 && <0.5) [details]
License BSD-3-Clause
Author michaelt
Maintainer andrew.thaddeus@gmail.com, what_is_it_to_do_anything@yahoo.com, colin@fosskers.ca
Category Data, Pipes, Streaming
Home page https://github.com/haskell-streaming/streaming-bytestring
Bug tracker https://github.com/haskell-streaming/streaming-bytestring/issues
Source repo head: git clone https://github.com/haskell-streaming/streaming-bytestring
Uploaded by fosskers at 2024-10-01T05:33:58Z
Distributions LTSHaskell:0.3.3, NixOS:0.3.2, Stackage:0.3.3
Reverse Dependencies 39 direct, 39 indirect [details]
Downloads 39625 total (257 in the last 30 days)
Rating 2.25 (votes: 2) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2024-10-01 [all 1 reports]

Readme for streaming-bytestring-0.3.3

[back to package description]

streaming-bytestring

Build Build Status Hackage

This library enables fast and safe streaming of byte data, in either Word8 or Char form. It is a core addition to the streaming ecosystem and avoids the usual pitfalls of combinbing lazy ByteStrings with lazy IO.

This library is used by streaming-attoparsec to enable vanilla Attoparsec parsers to work with streaming "for free".

Usage

Importing and Types

Modules from this library are intended to be imported qualified. To avoid conflicts with both the bytestring library and streaming, we recommended Q as the qualified name:

import qualified Streaming.ByteString.Char8 as Q

Like the bytestring library, leaving off the Char8 will expose an API based on Word8. Following the philosophy of streaming that "the best API is the one you already know", these APIs are based closely on bytestring. The core type is ByteStream m r, where:

  • m: The Monad used to fetch further chunks from the "source", usually IO.
  • r: The final return value after all streaming has concluded, usually () as in streaming.

You can imagine this type to represent an infinitely-sized collection of bytes, although internally it references a strict ByteString no larger than 32kb, followed by monadic instructions to fetch further chunks.

Examples

File Input

To open a file of any size and count its characters:

import Control.Monad.Trans.Resource (runResourceT)
import qualified Streaming.Streaming.Char8 as Q

-- | Represents a potentially-infinite stream of `Char`.
chars :: ByteStream IO ()
chars = Q.readFile "huge-file.txt"

main :: IO ()
main = runResourceT (Q.length_ chars) >>= print

Note that file IO specifically requires the resourcet library.

Line splitting and Stream interop

In the example above you may have noticed a lack of Of that we usually see with Stream. Our old friend lines hints at this too:

lines :: Monad m => ByteStream m r -> Stream (ByteStream m) m r

A stream-of-streams, yet no Of here either. The return type can't naively be Stream (Of ByteString) m r, since the first line break might be at the very end of a large file. Forcing that into a single strict ByteString would crash your program.

To count the number of lines whose first letter is i:

countOfI :: IO Int
countOfI = runResourceT
  . S.length_                   -- IO Int
  . S.filter (== 'i')           -- Stream (Of Char) IO ()
  . S.concat                    -- Stream (Of Char) IO ()
  . S.mapped Q.head             -- Stream (Of (Maybe Char)) IO ()
  . Q.lines                     -- Stream (ByteStream IO) IO ()
  $ Q.readFile "huge-file.txt"  -- ByteStream IO ()

Critically, there are several functions which when combined with mapped can bring us back into Of-land:

head     :: Monad m => ByteStream m r -> m (Of (Maybe Char) r)
last     :: Monad m => ByteStream m r -> m (Of (Maybe Char) r)
null     :: Monad m => ByteStream m r -> m (Of Bool) r)
count    :: Monad m => ByteStream m r -> m (Of Int) r)
toLazy   :: Monad m => ByteStream m r -> m (Of ByteString r) -- Be careful with this.
toStrict :: Monad m => ByteStream m r -> m (Of ByteString r) -- Be even *more* careful with this.

When moving in the opposite direction API-wise, consider:

fromChunks :: Stream (Of ByteString) m r -> ByteStream m r