{"action":"created","issue":{"url":"https://api.github.com/repos/well-typed/cborg/issues/156","repository_url":"https://api.github.com/repos/well-typed/cborg","labels_url":"https://api.github.com/repos/well-typed/cborg/issues/156/labels{/name}","comments_url":"https://api.github.com/repos/well-typed/cborg/issues/156/comments","events_url":"https://api.github.com/repos/well-typed/cborg/issues/156/events","html_url":"https://github.com/well-typed/cborg/issues/156","id":276055725,"number":156,"title":"Usable incremental deserialize API for streaming - not in ST moand","user":{"login":"ondrap","id":191226,"avatar_url":"https://avatars2.githubusercontent.com/u/191226?v=4","gravatar_id":"","url":"https://api.github.com/users/ondrap","html_url":"https://github.com/ondrap","followers_url":"https://api.github.com/users/ondrap/followers","following_url":"https://api.github.com/users/ondrap/following{/other_user}","gists_url":"https://api.github.com/users/ondrap/gists{/gist_id}","starred_url":"https://api.github.com/users/ondrap/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ondrap/subscriptions","organizations_url":"https://api.github.com/users/ondrap/orgs","repos_url":"https://api.github.com/users/ondrap/repos","events_url":"https://api.github.com/users/ondrap/events{/privacy}","received_events_url":"https://api.github.com/users/ondrap/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-11-22T12:48:46Z","updated_at":"2017-11-22T13:38:35Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"I wasn't following the development for some time and now I see the `deserialiseIncremental` runs in `ST` monad. This makes it quite unusable for any incremental situation as there is usually some (network, filesystem) read in between the decoding steps and e.g. in the absence of some length information I don't even know how much data is needed.\r\nIs there any deep reason to use the `ST` monad? Would it be possible to get back to the original approach without it?"},"comment":{"url":"https://api.github.com/repos/well-typed/cborg/issues/comments/346352241","html_url":"https://github.com/well-typed/cborg/issues/156#issuecomment-346352241","issue_url":"https://api.github.com/repos/well-typed/cborg/issues/156","id":346352241,"user":{"login":"bgamari","id":1010174,"avatar_url":"https://avatars0.githubusercontent.com/u/1010174?v=4","gravatar_id":"","url":"https://api.github.com/users/bgamari","html_url":"https://github.com/bgamari","followers_url":"https://api.github.com/users/bgamari/followers","following_url":"https://api.github.com/users/bgamari/following{/other_user}","gists_url":"https://api.github.com/users/bgamari/gists{/gist_id}","starred_url":"https://api.github.com/users/bgamari/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bgamari/subscriptions","organizations_url":"https://api.github.com/users/bgamari/orgs","repos_url":"https://api.github.com/users/bgamari/repos","events_url":"https://api.github.com/users/bgamari/events{/privacy}","received_events_url":"https://api.github.com/users/bgamari/received_events","type":"User","site_admin":false},"created_at":"2017-11-22T13:38:35Z","updated_at":"2017-11-22T13:38:35Z","author_association":"COLLABORATOR","body":"> Is there any deep reason to use the ST monad? \r\n\r\nIndeed there is: to allow principled use of mutation during deserialisation This is crucial to efficiently deserialsing dense structures like arrays.\r\n\r\nHowever, the use of `ST` is not at all at odds with streaming. Rather, you just need to use `Control.Monad.ST.stToIO` ([haddocks](https://hackage.haskell.org/package/base-4.10.0.0/docs/Control-Monad-ST.html#v:stToIO). For instance,\r\n\r\n```haskell\r\n{-# LANGUAGE TypeApplications #-}\r\nimport Codec.CBOR.Read\r\nimport Codec.Serialise.Class (decode)\r\nimport qualified Data.ByteString as BS\r\nimport System.IO\r\nimport Control.Monad.ST.Strict\r\n\r\nmain = withFile \"a-file\" ReadMode $ \\hdl -> do\r\n    s0 <- stToIO $ deserialiseIncremental (decode @(Maybe Int))\r\n    streamIt hdl s0\r\n\r\nstreamIt :: Handle -> IDecode RealWorld a -> IO a\r\nstreamIt hdl (Partial k) = do\r\n    dec <- BS.hGet hdl 16 >>= stToIO . k . Just\r\n    streamIt hdl dec\r\nstreamIt hdl (Done rest _ x) = return x\r\nstreamIt hdl (Fail rest _ e) = fail $ show e\r\n```"}}
{"actor":{"display_login":"bgamari"}}