3 ms·
> For instance, to actually encode a Cap’n Proto message, I couldn’t just allocate a buffer of zeros and then go through each field and set its value. Hah, de
by zvrba 13y ago
> For instance, to actually encode a Cap’n Proto message, I couldn’t just allocate a buffer of zeros and then go through each field and set its value.
Hah, dealing with native-encoded binary data in Haskell is rather cumbersome. I asked on r/haskell about decoding a native representation of float/double to corresponding Float/Double Haskell value, and this is the thread it generated:
http://www.reddit.com/r/haskell/comments/1k56i1/how_to_read_a_fp_number_from_a_binary_file/ http://www.reddit.com/r/haskell/comments/1k56i1/how_to_read_...
People have generally come with helpful suggestions, and rwbarton's excellent answer [check out the whole thread] has fully explained to me how to essentially reinterpret_cast a byte buffer.
I have been successfully playing with Haskell to implement some nontrivial _algorithms_ and liked. Like the OP, I'm now rethinking whether I want to invest more time into Haskell after seeing how cumbersome it can be to coerce the compiler into "DO THIS" where THIS is not a pure computation.
Now I'll take a pause from Haskell and start reading about Scala as it seems to provide a mix of FP and imperative (OO) that is more palatable to me.
- dons 13y agoI still don't understand why you even raised a reddit thread -- "Go with Data.Binary.IEEE754 and see if there's any reason not to like it -- it just works"
- zvrba 13y agoIs "genuine intellectual curiosity" a good enough answer? I tried to figure out by myself how to reinterpret_cast a ByteString to something else (Float/Double in my case). I used ByteString because it was recommended by #haskell for binary IO. I failed in my endeavor because the key function, which returns the pointer underlying ByteString, is in the hidden Internal subpackage. So I asked on reddit.
- dons 13y agoThe internal module isn't actually hidden. toForeignPtr should get you there: http://hackage.haskell.org/packages/archive/bytestring/0.9.1.4/doc/html/Data-ByteString-Internal.html#v%3AtoForeignPtr http://hackage.haskell.org/packages/archive/bytestring/0.9.1...
- zvrba 13y agoThis is a chicken-and-egg problem. Had I known about the existence of toForeignPtr [the link to Internal module doesn't even appear in the locally installed library docs], I would not have had started the reddit thread. Again, how was I supposed to know about the existence of Internal submodule and toForeignPtr function?
- dons 13y agoI suppose it should have gone like this: * You need to unmarshal hardware floats * You visit Hackage, and use the IEEE754 package. http://hackage.haskell.org/package/data-binary-ieee754 http://hackage.haskell.org/package/data-binary-ieee754 If that is fast enough, you stop. You are done. * IEEE754 is too slow for some reason, so you decide to do your own unsafeCoerce of bytestring * You look at the source for IEE754 and decide to marshal memory yourself: http://hackage.haskell.org/packages/archive/data-binary-ieee754/0.4.4/doc/html/src/Data-Binary-IEEE754.html#wordToDouble http://hackage.haskell.org/packages/archive/data-binary-ieee... * You read the bytestring package docs: http://hackage.haskell.org/packages/archive/bytestring/0.9.1.4/doc/html/Data-ByteString-Internal.html http://hackage.haskell.org/packages/archive/bytestring/0.9.1... and pay attention to the "unsafe" functions that cast raw pointers. * That lets you re-implement the IEEE754 package your own way The mistake here was not using the highly-optimized existing library - http://hackage.haskell.org/packages/archive/data-binary-ieee754/0.4.4/doc/html/src/Data-Binary-IEEE754.html#wordToDouble http://hackage.haskell.org/packages/archive/data-binary-ieee... - which already does the correct thing. You jumped straight to "have to roll my own low level binary parsing" and then started a reddit thread instead. None of that was necessary.
- zvrba 13y ago> "have to roll my own low level binary parsing" But that was the whole point from the beginning! Find out how can I do it myself. > You read the bytestring package docs: I repeat: the Internal module isn't listed in the locally installed documentation, and the link on Hackage is broken. Do I really need to produce for you the two top-level links where I was looking for the documentation of Internal?