Re: [Haskell-cafe] Help needed for converting IOArray to ByteString

2011-02-17 Thread Henning Thielemann
On Tue, 8 Feb 2011, C K Kashyap wrote: I need to convert IOArray to bytestring as shown below -  import Data.Array.IO import Data.Binary.Put import qualified Data.ByteString.Lazy as BS import Data.Word main = do arr - newArray (0,9) 0 :: IO (IOArray Int Int) let bs=toByteString arr return ()

[Haskell-cafe] Help needed for converting IOArray to ByteString

2011-02-08 Thread C K Kashyap
Hi, I need to convert IOArray to bytestring as shown below - import Data.Array.IO import Data.Binary.Put import qualified Data.ByteString.Lazy as BS import Data.Word main = do arr - newArray (0,9) 0 :: IO (IOArray Int Int) let bs=toByteString arr return () How can I implement the 'toByteString'

Re: [Haskell-cafe] Help needed for converting IOArray to ByteString

2011-02-08 Thread Michael Snoyman
Your array contains machine-sized Ints, which in practice are likely either 32-bit or 64-bit, while a ByteString is the equivalent of an array or 8-bit values. So you'll need to somehow convert the Ints to Word8s. Do you know if you need big or little endian? A basic approach would be: * Use

Re: [Haskell-cafe] Help needed for converting IOArray to ByteString

2011-02-08 Thread C K Kashyap
On Tue, Feb 8, 2011 at 2:26 PM, Michael Snoyman mich...@snoyman.com wrote: Your array contains machine-sized Ints, which in practice are likely either 32-bit or 64-bit, while a ByteString is the equivalent of an array or 8-bit values. So you'll need to somehow convert the Ints to Word8s. Do

Re: [Haskell-cafe] Help needed for converting IOArray to ByteString

2011-02-08 Thread Michael Snoyman
On Tue, Feb 8, 2011 at 11:13 AM, C K Kashyap ckkash...@gmail.com wrote: On Tue, Feb 8, 2011 at 2:26 PM, Michael Snoyman mich...@snoyman.com wrote: Your array contains machine-sized Ints, which in practice are likely either 32-bit or 64-bit, while a ByteString is the equivalent of an array

Re: [Haskell-cafe] Help needed for converting IOArray to ByteString

2011-02-08 Thread C K Kashyap
1) Just use Data.Word.Word8 instead of the second Int in your type sig for IOArray 2) Use getElems to get a [Word8] 3) Data.ByteString.pack converts a [Word8] into a ByteString Michael I am currently using a list of tuples - [(Int,Int,Int)] to represent an image buffer. You can see it in

Re: [Haskell-cafe] Help needed for converting IOArray to ByteString

2011-02-08 Thread Ketil Malde
C K Kashyap ckkash...@gmail.com writes: I am currently using a list of tuples - [(Int,Int,Int)] to represent an image buffer. [...] Looks like this is pretty slow, Unsurprisingly, as there's a huge space overhead, and (depending on usage, but probably even worse) linear access time. I

Re: [Haskell-cafe] Help needed for converting IOArray to ByteString

2011-02-08 Thread Gábor Lehel
On Tue, Feb 8, 2011 at 10:39 AM, C K Kashyap ckkash...@gmail.com wrote: 1) Just use Data.Word.Word8 instead of the second Int in your type sig for IOArray 2) Use getElems to get a [Word8] 3) Data.ByteString.pack converts a [Word8] into a ByteString Michael I am currently using a list of