@@ -47,7 +47,8 @@ module Data.Vector.Generic.Mutable (
4747 read , write , modify , modifyM , swap , exchange ,
4848 unsafeRead , unsafeWrite , unsafeModify , unsafeModifyM , unsafeSwap , unsafeExchange ,
4949
50- -- * Folds
50+ -- * Maps and folds
51+ mapM , imapM , forM , iforM ,
5152 mapM_ , imapM_ , forM_ , iforM_ ,
5253 foldl , foldl' , foldM , foldM' ,
5354 foldr , foldr' , foldrM , foldrM' ,
@@ -88,7 +89,7 @@ import Data.Vector.Internal.Check
8889import Control.Monad.Primitive ( PrimMonad (.. ), RealWorld , stToPrim )
8990
9091import Prelude hiding ( length , null , replicate , reverse , map , read ,
91- take , drop , splitAt , init , tail , mapM_ , foldr , foldl )
92+ take , drop , splitAt , init , tail , mapM , mapM_ , foldr , foldl )
9293
9394#include "vector.h"
9495
@@ -727,6 +728,44 @@ unsafeExchange v i x = checkIndex Unsafe i (length v) $ stToPrim $ do
727728-- Folds
728729-- -----
729730
731+ forI :: (PrimMonad m , MVector v a ) => v (PrimState m ) a -> (Int -> m a ) -> m ()
732+ {-# INLINE forI #-}
733+ forI v f = loop 0
734+ where
735+ loop i | i >= n = return ()
736+ | otherwise = f i >>= unsafeWrite v i >> loop (i + 1 )
737+ n = length v
738+
739+ -- | /O(n)/ Apply the monadic action to every element of the vector, modifying it.
740+ --
741+ -- @since 0.13.0.1
742+ mapM :: (PrimMonad m , MVector v a ) => (a -> m a ) -> v (PrimState m ) a -> m ()
743+ {-# INLINE mapM #-}
744+ mapM f v = forI v $ \ i -> f =<< unsafeRead v i
745+
746+ -- | /O(n)/ Apply the monadic action to every element of the vector and its index, modifying the vector.
747+ --
748+ -- @since 0.13.0.1
749+ imapM :: (PrimMonad m , MVector v a ) => (Int -> a -> m a ) -> v (PrimState m ) a -> m ()
750+ {-# INLINE imapM #-}
751+ imapM f v = forI v $ \ i -> f i =<< unsafeRead v i
752+
753+ -- | /O(n)/ Apply the monadic action to every element of the vector,
754+ -- modifying it. It's the same as @flip mapM_@.
755+ --
756+ -- @since 0.13.0.1
757+ forM :: (PrimMonad m , MVector v a ) => v (PrimState m ) a -> (a -> m a ) -> m ()
758+ {-# INLINE forM #-}
759+ forM = flip mapM
760+
761+ -- | /O(n)/ Apply the monadic action to every element of the vector
762+ -- and its index, modifying the vector. It's the same as @flip imapM_@.
763+ --
764+ -- @since 0.13.0.1
765+ iforM :: (PrimMonad m , MVector v a ) => v (PrimState m ) a -> (Int -> a -> m a ) -> m ()
766+ {-# INLINE iforM #-}
767+ iforM = flip imapM
768+
730769forI_ :: (Monad m , MVector v a ) => v (PrimState m ) a -> (Int -> m b ) -> m ()
731770{-# INLINE forI_ #-}
732771forI_ v f = loop 0
@@ -736,29 +775,35 @@ forI_ v f = loop 0
736775 n = length v
737776
738777-- | /O(n)/ Apply the monadic action to every element of the vector, discarding the results.
778+ -- The vector is not modified.
739779--
740780-- @since 0.12.3.0
741781mapM_ :: (PrimMonad m , MVector v a ) => (a -> m b ) -> v (PrimState m ) a -> m ()
742782{-# INLINE mapM_ #-}
743783mapM_ f v = forI_ v $ \ i -> f =<< unsafeRead v i
744784
745785-- | /O(n)/ Apply the monadic action to every element of the vector and its index, discarding the results.
786+ -- The vector is not modified.
746787--
747788-- @since 0.12.3.0
748789imapM_ :: (PrimMonad m , MVector v a ) => (Int -> a -> m b ) -> v (PrimState m ) a -> m ()
749790{-# INLINE imapM_ #-}
750791imapM_ f v = forI_ v $ \ i -> f i =<< unsafeRead v i
751792
752793-- | /O(n)/ Apply the monadic action to every element of the vector,
753- -- discarding the results. It's the same as @flip mapM_@.
794+ -- discarding the results. The vector is not modified.
795+ --
796+ -- It's the same as @flip mapM_@.
754797--
755798-- @since 0.12.3.0
756799forM_ :: (PrimMonad m , MVector v a ) => v (PrimState m ) a -> (a -> m b ) -> m ()
757800{-# INLINE forM_ #-}
758801forM_ = flip mapM_
759802
760803-- | /O(n)/ Apply the monadic action to every element of the vector
761- -- and its index, discarding the results. It's the same as @flip imapM_@.
804+ -- and its index, discarding the results. The vector is not modified.
805+ --
806+ -- It's the same as @flip imapM_@.
762807--
763808-- @since 0.12.3.0
764809iforM_ :: (PrimMonad m , MVector v a ) => v (PrimState m ) a -> (Int -> a -> m b ) -> m ()
0 commit comments