@@ -58,6 +58,7 @@ module Data.Vector.Generic.Mutable (
5858 ifoldr , ifoldr' , ifoldrM , ifoldrM' ,
5959
6060 -- * Modifying vectors
61+ mapInPlace , imapInPlace , mapInPlaceM , imapInPlaceM ,
6162 nextPermutation , nextPermutationBy ,
6263 prevPermutation , prevPermutationBy ,
6364
@@ -77,6 +78,7 @@ module Data.Vector.Generic.Mutable (
7778 PrimMonad , PrimState , RealWorld
7879) where
7980
81+ import Control.Monad ((<=<) )
8082import Data.Vector.Generic.Mutable.Base
8183import qualified Data.Vector.Generic.Base as V
8284
@@ -1215,6 +1217,37 @@ partitionWithUnknown f s
12151217-- Modifying vectors
12161218-- -----------------
12171219
1220+ -- | Modify vector in place by applying function to each element.
1221+ --
1222+ -- @since NEXT_VERSION
1223+ mapInPlace :: (PrimMonad m , MVector v a ) => (a -> a ) -> v (PrimState m ) a -> m ()
1224+ {-# INLINE mapInPlace #-}
1225+ mapInPlace f = imapInPlace (\ _ -> f)
1226+
1227+ -- | Modify vector in place by applying function to each element and its index.
1228+ --
1229+ -- @since NEXT_VERSION
1230+ imapInPlace :: (PrimMonad m , MVector v a ) => (Int -> a -> a ) -> v (PrimState m ) a -> m ()
1231+ {-# INLINE imapInPlace #-}
1232+ imapInPlace f v
1233+ = stToPrim $ iforM_ v $ \ i -> unsafeWrite v i . f i
1234+
1235+ -- | Modify vector in place by applying monadic function to each element in order.
1236+ --
1237+ -- @since NEXT_VERSION
1238+ mapInPlaceM :: (PrimMonad m , MVector v a ) => (a -> m a ) -> v (PrimState m ) a -> m ()
1239+ {-# INLINE mapInPlaceM #-}
1240+ mapInPlaceM f
1241+ = imapInPlaceM (\ _ -> f)
1242+
1243+ -- | Modify vector in place by applying monadic function to each element and its index in order.
1244+ --
1245+ -- @since NEXT_VERSION
1246+ imapInPlaceM :: (PrimMonad m , MVector v a ) => (Int -> a -> m a ) -> v (PrimState m ) a -> m ()
1247+ {-# INLINE imapInPlaceM #-}
1248+ imapInPlaceM f v
1249+ = iforM_ v $ \ i -> unsafeWrite v i <=< f i
1250+
12181251
12191252-- | Compute the (lexicographically) next permutation of the given vector in-place.
12201253-- Returns False when the input is the last item in the enumeration, i.e., if it is in
0 commit comments