Skip to content

Commit 4ab70cb

Browse files
committed
Add Array.Generic.replicate
1 parent 1be8b88 commit 4ab70cb

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

  • core/src/Streamly/Internal/Data/MutArray

core/src/Streamly/Internal/Data/MutArray/Generic.hs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module Streamly.Internal.Data.MutArray.Generic
1717
-- * Constructing and Writing
1818
-- ** Construction
1919
, nil
20+
, replicate
2021

2122
-- *** Uninitialized Arrays
2223
, emptyOf
@@ -210,7 +211,7 @@ import qualified Streamly.Internal.Data.Stream.Generate as D
210211
import qualified Streamly.Internal.Data.Stream.Lift as D
211212
import qualified Streamly.Internal.Data.StreamK.Type as K
212213

213-
import Prelude hiding (read, length)
214+
import Prelude hiding (read, length, replicate)
214215

215216
#include "DocTestDataMutArrayGeneric.hs"
216217

@@ -250,20 +251,29 @@ bottomElement =
250251
-- XXX Would be nice if GHC can provide something like newUninitializedArray# so
251252
-- that we do not have to write undefined or error in the whole array.
252253

254+
{-# INLINE replicateWithEnd #-}
255+
replicateWithEnd :: MonadIO m => Int -> Int -> a -> m (MutArray a)
256+
replicateWithEnd n@(I# n#) end val =
257+
liftIO
258+
$ IO
259+
$ \s# ->
260+
case newArray# n# val s# of
261+
(# s1#, arr# #) ->
262+
let ma = MutArray arr# 0 end n
263+
in (# s1#, ma #)
264+
265+
266+
{-# INLINE replicate #-}
267+
replicate :: MonadIO m => Int -> a -> m (MutArray a)
268+
replicate n = replicateWithEnd n n
269+
253270
-- | @emptyOf count@ allocates a zero length array that can be extended to hold
254271
-- up to 'count' items without reallocating.
255272
--
256273
-- /Pre-release/
257274
{-# INLINE emptyOf #-}
258275
emptyOf :: MonadIO m => Int -> m (MutArray a)
259-
emptyOf n@(I# n#) =
260-
liftIO
261-
$ IO
262-
$ \s# ->
263-
case newArray# n# bottomElement s# of
264-
(# s1#, arr# #) ->
265-
let ma = MutArray arr# 0 0 n
266-
in (# s1#, ma #)
276+
emptyOf n = replicateWithEnd n 0 bottomElement
267277

268278
{-# DEPRECATED new "Please use emptyOf instead." #-}
269279
{-# INLINE new #-}

0 commit comments

Comments
 (0)