Skip to content

Instantly share code, notes, and snippets.

@qnikst
Last active August 27, 2019 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qnikst/5a436ac4f22b47b24fba7f73e1a64d49 to your computer and use it in GitHub Desktop.
Save qnikst/5a436ac4f22b47b24fba7f73e1a64d49 to your computer and use it in GitHub Desktop.
import Data.Binary
import Data.Binary.Get
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
stream :: Binary a => BL.ByteString -> [a]
stream = go (runGetIncremental get) . BL.toChunks where
go (Done rest _ a) []
| B.null rest = [a]
| otherwise = a:go (runGetIncremental get `pushChunk` rest) []
go (Done rest _ a) (x:xs)
| B.null rest = a:go (runGetIncremental get `pushChunk` x) xs
| otherwise = a:go (runGetIncremental get ` pushChunk` rest) (x:xs)
go (Partial f) [] = go (f Nothing) []
go (Partial f) (x:xs) = go (f (Just x)) xs
go (Fail _ _ s) _ = error s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment