on master help sign in
implement overlay in Array APIs

Fix review comments Fix import order

commit020be2436763013e0919256066a511bc209734dd authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateSun 26 Sep 2021, 18:03 (5 years ago) parentf906866
1 file changed+19
src/Streamly/Internal/Data/Array.hs+19
src/Streamly/Internal/Data/Array.hs+19
@@ -46,6 +46,7 @@ module Streamly.Internal.Data.Array
4646 -- * Random Access
4747 , getIndexUnsafe
4848 , strip
49+ , putIndices
4950 )
5051where
5152
@@ -54,6 +55,7 @@ import Control.DeepSeq (NFData(..))
5455#endif
5556import Control.Monad (when)
5657import Control.Monad.IO.Class (liftIO, MonadIO)
58+import Control.Monad.Primitive (PrimMonad)
5759import Data.Functor.Identity (runIdentity)
5860import Data.IORef
5961import GHC.Base (Int(..))
@@ -313,3 +315,20 @@ strip p arr =
313315 if p (indexArray arr idx) then getIndexR (idx - 1) else idx
314316
315317 getIndexL idx = if p (indexArray arr idx) then getIndexL (idx + 1) else idx
318+
319+-- | Write an input stream of (index, value) pairs to an array. Throws an
320+-- error if any index is out of bounds.
321+--
322+-- /Pre-release/
323+{-# INLINE putIndices #-}
324+putIndices :: PrimMonad m
325+ => Array a -> Fold m (Int, a) ()
326+putIndices arr = FL.rmapM (\ _ -> return ()) (FL.foldlM' step initial)
327+
328+ where
329+
330+ initial = unsafeThawArray arr
331+
332+ step marr (i, x) = do
333+ writeArray marr i x
334+ return marr