Examples of using STUArray + help with converting code

Folks, Are there any examples on using STUArray and friends? I'm trying to convert the following bit of code which uses deprecated features. I don't understand the syntax needed to create a new double or float array with newArray from Data.Array.MArray. I also don't yet understand how to cast that double array to read ints from it. doubleToInts d = runST ( do arr <- newDoubleArray (1,2) writeDoubleArray arr 1 d i1 <- readIntArray arr 1 i2 <- readIntArray arr 2 return (i1,i2)) Thanks, Joel -- http://wagerlabs.com/ -- http://wagerlabs.com/

On 10/13/05, joel reymont
Folks,
Are there any examples on using STUArray and friends? I'm trying to convert the following bit of code which uses deprecated features.
I don't understand the syntax needed to create a new double or float array with newArray from Data.Array.MArray.
do arr <- newArray_ (1,2) :: ST s (STUArray s Int Double) writeArray arr 1 d ... The type signature is to remove the ambiguity in type-class constraints.
I also don't yet understand how to cast that double array to read ints from it.
Don't. Best regards Tomasz

If I don't cast then how do I convert this code? doubleToInts d = runST ( do arr <- newDoubleArray (1,2) writeDoubleArray arr 1 d i1 <- readIntArray arr 1 i2 <- readIntArray arr 2 return (i1,i2)) Or can I just read an array of ints from the double array using the new polymorphic approach? Thanks, Joel On Oct 13, 2005, at 7:09 PM, Tomasz Zielonka wrote:
I also don't yet understand how to cast that double array to read ints from it.
Don't.

Joel Reymont
If I don't cast then how do I convert this code?
Uh, what is wrong with divMod? *Main Data.Word> (10000000000::Word64) `divMod` (2^32) (2,1410065408)
doubleToInts d = runST ( [...]
This will only give you a headache. :-) -k -- If I haven't seen further, it is by standing in the footprints of giants

I must be missing something because I don't think the code below converts a double. Then there's this other bit to consider: intsToDouble (i1,i2) = runST ( do arr <- newDoubleArray (1,2) writeIntArray arr 1 i1 writeIntArray arr 2 i2 readDoubleArray arr 1) I'm just looking for the new syntax, the one above is deprecated. Thanks, Joel On Oct 14, 2005, at 2:31 PM, Ketil Malde wrote:
Uh, what is wrong with divMod?
*Main Data.Word> (10000000000::Word64) `divMod` (2^32) (2,1410065408)

Joel Reymont
I must be missing something because I don't think the code below converts a double.
Yes, sorry, my bad. I was (and is) confused about what you wanted to do. -k -- If I haven't seen further, it is by standing in the footprints of giants

I'm just trying to replicate the example using the "fresh" syntax that does not use readDoubleArray, readIntArray, etc. On Oct 14, 2005, at 4:32 PM, Ketil Malde wrote:
Yes, sorry, my bad. I was (and is) confused about what you wanted to do.

joel reymont wrote:
I don't understand the syntax needed to create a new double or float array with newArray from Data.Array.MArray. I also don't yet understand how to cast that double array to read ints from it.
doubleToInts d = runST ( do arr <- newDoubleArray (1,2) writeDoubleArray arr 1 d i1 <- readIntArray arr 1 i2 <- readIntArray arr 2 return (i1,i2))
Just use newArray, writeArray and readArray. Something (writeArray is probably the easiest) will need an explicit type signature to resolve the overloading. Also from the GHC docs: castSTUArray :: STUArray s ix a -> ST s (STUArray s ix b) Casts an STUArray with one element type into one with a different element type. All the elements of the resulting array are undefined (unless you know what you're doing...). Note the part in parentheses. This is Haskell, not C after all. A clean solution may not be that far away. Udo. -- I underthand that people theem to like it better now. That it ith written in thee pluth pluth inthtead of lithp.
participants (5)
-
joel reymont
-
Joel Reymont
-
Ketil Malde
-
Tomasz Zielonka
-
Udo Stenzel