[Haskell-cafe] replicateM over vectors

Don Stewart dons at galois.com
Thu Apr 1 22:01:14 EDT 2010


rl:
> 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)


Hmm, but here 'a' is pure. I don't think he wants

    newWith :: (PrimMonad m, MVector v a) => Int -> a -> m (v (PrimState m) a)

but more:

    newWithM :: (PrimMonad m, MVector v a) => Int -> m a -> m (v (PrimState m) a)

  
> 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
> 

Oh, that's interesting. But what if we want to fill directly with the monadic action?
We wouldn't

    mapM (const a) $ replicate n undefined 

So how do we best do a fusible, e.g.:

    replicateM :: G.Vector v a => Int -> IO a -> IO (v a)

-- Don


More information about the Haskell-Cafe mailing list