[Haskell-cafe] replicateM over vectors

Scherrer, Chad Chad.Scherrer at pnl.gov
Thu Apr 1 17:48:05 EDT 2010


Hi,

I'd like to be able to do replicateM, but over a vector instead of a list. Right now I'm doing this:
 
import qualified Data.Vector.Generic as G
import qualified Data.Vector.Unboxed.Mutable as M
replicateM n action = do
  mu <- M.unsafeNew n
  let go !i | i < n = action >>= M.unsafeWrite mu i >> go (i+1)
             | otherwise = G.unsafeFreeze mu
  go 0

I thought it might be useful to express this in terms of the available primitives, since this might fuse more easily, but I don't yet see a way to do it.

Is there a better (more elegant and/or faster) way to write this?

Thanks,
Chad


More information about the Haskell-Cafe mailing list