on master help sign in
Implement putIndices
b5bf22bRanjeet Kumar Ranjan
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+17−8
@@ -547,14 +547,6 @@ withNewArrayUnsafe count f = do
547-- Random writes
547-- Random writes
548-------------------------------------------------------------------------------
548-------------------------------------------------------------------------------
549
549
550−-- | Write an input stream of (index, value) pairs to an array. Throws an
551−-- error if any index is out of bounds.
552−--
553−-- /Unimplemented/
554−{-# INLINE putIndices #-}
555−putIndices :: Array a -> Fold m (Int, a) ()
556−putIndices = undefined
557−
558-- | Write the given element to the given index of the array. Does not check if
550-- | Write the given element to the given index of the array. Does not check if
559-- the index is out of bounds of the array.
551-- the index is out of bounds of the array.
560--
552--
@@ -597,6 +589,23 @@ putIndex arr i x =
597 unsafeWithArrayContents (arrContents arr) (arrStart arr)
589 unsafeWithArrayContents (arrContents arr) (arrStart arr)
598 $ \p -> putIndexPtr p (aEnd arr) i x
590 $ \p -> putIndexPtr p (aEnd arr) i x
599
591
592+-- | Write an input stream of (index, value) pairs to an array. Throws an
593+-- error if any index is out of bounds.
594+--
595+-- /Pre-release/
596+{-# INLINE putIndices #-}
597+putIndices :: forall m a. (MonadIO m, Storable a)
598+ => Array a -> Fold m (Int, a) ()
599+putIndices Array{..} = FL.mkFoldM step initial extract
600+
601+ where
602+
603+ initial = return $ FL.Partial ()
604+
605+ step () (i, x) = FL.Partial <$> liftIO (putIndexPtr arrStart aEnd i x)
606+
607+ extract () = liftIO $ touch arrContents
608+
600-- | Modify a given index of an array using a modifier function.
609-- | Modify a given index of an array using a modifier function.
601--
610--
602-- /Pre-release/
611-- /Pre-release/