on master help sign in
Implement modify
commit77998d14eb57b8b3661e9d82a2b45bcf81043760 authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateMon 27 Dec 2021, 20:20 (5 years ago) parent3ffd661
1 file changed+14−4
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+14−4
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+14−4
@@ -635,10 +635,20 @@ modifyIndices = undefined
635635
636636-- | Modify each element of an array using the supplied modifier function.
637637--
638−-- /Unimplemented/
639−modify :: -- forall m a b. (MonadIO m, Storable a) =>
640− Array a -> (a -> a) -> m ()
641−modify = undefined
638+-- /Pre-release/
639+modify :: forall m a. (MonadIO m, Storable a)
640+ => Array a -> (a -> a) -> m ()
641+modify Array{..} f = liftIO $
642+ unsafeWithArrayContents arrContents arrStart go
643+
644+ where
645+
646+ go ptr = do
647+ let elemSize = sizeOf (undefined :: a)
648+ when (ptr `plusPtr` elemSize <= aEnd) $ do
649+ r <- peek ptr
650+ poke ptr (f r)
651+ go (ptr `plusPtr` elemSize)
642652
643653-- | Swap the elements at two indices.
644654--