module PLY.Internal.StrictReplicate where
import Data.Vector.Generic (Vector, unstream)
import Data.Vector.Fusion.Stream (unsafeFromList)
import Data.Vector.Fusion.Stream.Monadic (Stream (..), Step(..), size, toList)
import Data.Vector.Fusion.Util (delay_inline)
import Data.Vector.Fusion.Stream.Size (Size(..))
replicateStreamM' :: Monad m => Int -> m a -> Stream m a
replicateStreamM' n p = Stream step n (Exact (delay_inline max n 0))
where
step i | i <= 0 = return Done
| otherwise = do { !x <- p; return $ Yield x (i1) }
replicateM' :: (Monad m, Vector v a) => Int -> m a -> m (v a)
replicateM' n p = do let s = replicateStreamM' n p
xs <- toList s
return . unstream $ unsafeFromList (size s) xs