[Haskell-cafe] Array copying
Andrew Coppin
andrewcoppin at btinternet.com
Wed Dec 5 06:04:02 EST 2007
Jules Bean wrote:
> Andrew Coppin wrote:
>> Andrew Coppin wrote:
>>> copy :: Word32 -> IOUArray Word32 Bool -> Word32 -> IO (IOUArray
>>> Word32 Bool)
>>> copy p grid size = do
>>> let size' = size * p
>>> grid' <- newArray (1,size') False
>>>
>>> mapM_
>>> (\n -> do
>>> b <- readArray grid n
>>> if b
>>> then mapM_ (\x -> writeArray grid' (n + size*x) True) [0..p-1]
>>> else return ()
>>> )
>>> [1..size]
>>>
>>> return grid'
>>
>> Actually, thinking about this... for most kinds of arrays (whether
>> boxed or unboxed, mutable or immutable) there's probably a more
>> efficient way to copy the data then this. Maybe we should add
>> something to the various array APIs to allow efficient copying of
>> arrays / large chunks of arrays?
>
> Ideally we'd have the compiler generate optimal code anyway, then you
> wouldn't need such things.
>
> So, let's hope those compiler/optimiser guys keep working hard!
True - but "copyArray x y" is surely a lot clearer than some longwinded
mapM thing, no? ;-)
More information about the Haskell-Cafe
mailing list