https://github.com/psibi/streamly-bytestring
https://github.com/psibi/streamly-bytestring
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/psibi/streamly-bytestring
- Owner: psibi
- License: bsd-3-clause
- Created: 2019-06-01T19:23:42.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-07-20T20:15:01.000Z (almost 2 years ago)
- Last Synced: 2024-10-11T23:59:38.885Z (over 1 year ago)
- Language: Haskell
- Size: 92.8 KB
- Stars: 9
- Watchers: 8
- Forks: 8
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.md
- License: LICENSE
Awesome Lists containing this project
README
# streamly-bytestring
Library for streamly and bytestring interoperation.
If you are writing code from scratch, please use `Streamly.Data.Array` which is
a generalization of `ByteString` and better integrated with streamly.
This library is to enable interoperation of streamly with existing code that
uses `ByteString`.
The package provides APIs to interconvert between strict `Bytestring` and
streamly `Array Word8` and between lazy `Bytestring` and stream of `Array
Word8`.
The interconversion in the case of strict `Bytestring` and streamly `Array
Word8` has no overhead for GHC allocated memory. For foreign allocator allocated
memory there is a copy involved.
## Usage
This is a dumb program that counts the number of bytes in a file.
```haskell
import qualified Streamly.Data.Stream as S
import qualified Streamly.Data.Fold as FL
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import qualified Streamly.External.ByteString as Strict
import qualified Streamly.External.ByteString.Lazy as Lazy
import System.IO (FilePath)
strictByteStringSize :: BS.ByteString -> IO Int
strictByteStringSize bs = S.fold FL.length $ S.unfold Strict.reader bs
lazyByteStringSize :: BSL.ByteString -> IO Int
lazyByteStringSize bsl =
S.fold (FL.foldl' (+) 0)
$ S.mapM strictByteStringSize
$ fmap Strict.fromArray
$ Lazy.toChunks bsl
fileSize :: FilePath -> IO Int
fileSize path = do
bsl <- BSL.readFile path
lazyByteStringSize bsl
```