[Haskell-cafe] replicateM over vectors

Roman Leshchinskiy rl at cse.unsw.edu.au
Thu Apr 1 21:46:29 EDT 2010

On 02/04/2010, at 12:16, Don Stewart wrote:

> Chad.Scherrer:
>> Hi,
>> I'd like to be able to do replicateM, but over a vector instead of a list. Right now I'm doing this:

The operation you are looking for is called newWith. It probably should be called replicate.

> Roman? Can we generate frozen arrays for monadic generators, and still fuse in
> the current New/Mutable/MStream architecture?

For monadic stuff, fusion happens on things of type New. For instance, you could write this (I'm omitting the class contexts and Data.Vector prefixes):

replicate :: Int -> a -> New a
replicate n x = Generic.New.unstream (Fusion.Stream.replicate n x)

and then either

  Mutable.run (replicate n x)

to get a mutable vector or

  new (replicate n x)

to get an immutable one. You could also chain operations on New, including monadic ones:

  v <- Mutable.run $ Generic.New.transform (Fusion.Stream.Monadic.mapM f)
                   $ replicate n x

and this ought to fuse.

Note that the New stuff is quite rough and only really intended for internal use at the moment. I wanted to get the "normal" APIs working properly first.


More information about the Haskell-Cafe mailing list