stringfmt
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Fmt.ByteString.Lazy

Description

Lazy ByteString as Mu (Cons StrictByteString).

Lazy ByteString is internally a linked list of strict chunks:

data ByteString = Empty | Chunk !StrictByteString ByteString

This is exactly Fix (Cons StrictByteString). This module provides conversions and operations that let you apply the full recursion scheme and streaming metamorphism infrastructure to lazy ByteStrings.

import Data.Fmt.ByteString.Lazy

-- Fold a lazy ByteString chunk by chunk
foldChunks (\case Nil -> 0; Cons chunk n -> BS.length chunk + n) myLBS

-- Stream-transform chunks
transformChunks (fstream unwrap wrap produce consume flush state) myLBS
Synopsis

Conversion

toChunks :: ByteString -> Mu (Cons ByteString) Source #

View a lazy ByteString as Mu (Cons ByteString).

Each Cons layer holds one strict chunk.

O(n) — rebuilds the spine as a Church-encoded list.

fromChunks :: Mu (Cons ByteString) -> ByteString Source #

Reassemble a Mu (Cons ByteString) into a lazy ByteString.

O(n) — folds the Church-encoded list back into chunks.

toNuChunks :: ByteString -> Nu (Cons ByteString) Source #

View a lazy ByteString as Nu (Cons ByteString) for lazy streaming. O(1) construction — the ByteString itself is the seed.

Folding

foldChunks :: Algebra (Cons ByteString) a -> ByteString -> a Source #

Fold a lazy ByteString chunk by chunk.

foldChunks alg = fold alg . toChunks
>>> foldChunks (\case Nil -> 0; Cons c n -> BS.length c + n) myLBS

Unfolding

unfoldChunks :: Coalgebra (Cons ByteString) a -> a -> ByteString Source #

Build a lazy ByteString from a seed, one chunk at a time.

unfoldChunks coalg = fromChunks . unfold coalg

Refold

refoldChunks :: Algebra (Cons ByteString) b -> Coalgebra (Cons ByteString) a -> a -> b Source #

Unfold then fold, fused — no intermediate Mu structure.

refoldChunks alg coalg = foldChunks alg . unfoldChunks coalg but without materializing the Church-encoded list.

Mapping

mapChunks :: (ByteString -> ByteString) -> ByteString -> ByteString Source #

Map a function over each strict chunk.

>>> mapChunks (BS.map toUpper) myLBS

Streaming

streamChunks :: (Mu (Cons ByteString) -> Mu (Cons ByteString)) -> ByteString -> ByteString Source #

Apply a streaming transformation to the chunks.

Takes a function that operates on Mu (Cons ByteString) and lifts it to operate on lazy ByteString.

transformChunks :: (Mu (Cons ByteString) -> Mu (Cons a)) -> ByteString -> [a] Source #

Apply a streaming transformation that changes the element type.

Useful for encoding/decoding pipelines where the chunk type changes.