Skip to content

Commit e7ea6a8

Browse files
committed
add Mutable.mapM et al, clarify that Mutable.mapM_ does not modify
1 parent 1696820 commit e7ea6a8

File tree

2 files changed

+90
-8
lines changed

2 files changed

+90
-8
lines changed

vector/src/Data/Vector/Generic/Mutable.hs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
8889
import Control.Monad.Primitive ( PrimMonad(..), RealWorld, stToPrim )
8990

9091
import 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+
730769
forI_ :: (Monad m, MVector v a) => v (PrimState m) a -> (Int -> m b) -> m ()
731770
{-# INLINE forI_ #-}
732771
forI_ 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
741781
mapM_ :: (PrimMonad m, MVector v a) => (a -> m b) -> v (PrimState m) a -> m ()
742782
{-# INLINE mapM_ #-}
743783
mapM_ 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
748789
imapM_ :: (PrimMonad m, MVector v a) => (Int -> a -> m b) -> v (PrimState m) a -> m ()
749790
{-# INLINE imapM_ #-}
750791
imapM_ 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
756799
forM_ :: (PrimMonad m, MVector v a) => v (PrimState m) a -> (a -> m b) -> m ()
757800
{-# INLINE forM_ #-}
758801
forM_ = 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
764809
iforM_ :: (PrimMonad m, MVector v a) => v (PrimState m) a -> (Int -> a -> m b) -> m ()

vector/src/Data/Vector/Mutable.hs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ module Data.Vector.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',
@@ -74,7 +75,7 @@ import Data.Primitive.Array
7475
import Control.Monad.Primitive
7576

7677
import Prelude hiding ( length, null, replicate, reverse, read,
77-
take, drop, splitAt, init, tail, foldr, foldl, mapM_ )
78+
take, drop, splitAt, init, tail, foldr, foldl, mapM, mapM_ )
7879

7980
import Data.Typeable ( Typeable )
8081

@@ -552,30 +553,66 @@ nextPermutation = G.nextPermutation
552553
-- Folds
553554
-- -----
554555

556+
-- | /O(n)/ Apply the monadic action to every element of the vector, modifying it.
557+
--
558+
-- @since 0.13.0.1
559+
mapM :: (PrimMonad m) => (a -> m a) -> MVector (PrimState m) a -> m ()
560+
{-# INLINE mapM #-}
561+
mapM = G.mapM
562+
563+
-- | /O(n)/ Apply the monadic action to every element of the vector and its index, modifying the vector.
564+
--
565+
-- @since 0.13.0.1
566+
imapM :: (PrimMonad m) => (Int -> a -> m a) -> MVector (PrimState m) a -> m ()
567+
{-# INLINE imapM #-}
568+
imapM = G.imapM
569+
570+
-- | /O(n)/ Apply the monadic action to every element of the vector,
571+
-- modifying it. It's the same as @flip mapM_@.
572+
--
573+
-- @since 0.13.0.1
574+
forM :: (PrimMonad m) => MVector (PrimState m) a -> (a -> m a) -> m ()
575+
{-# INLINE forM #-}
576+
forM = G.forM
577+
578+
-- | /O(n)/ Apply the monadic action to every element of the vector
579+
-- and its index, modifying the vector. It's the same as @flip imapM_@.
580+
--
581+
-- @since 0.13.0.1
582+
iforM :: (PrimMonad m) => MVector (PrimState m) a -> (Int -> a -> m a) -> m ()
583+
{-# INLINE iforM #-}
584+
iforM = G.iforM
585+
555586
-- | /O(n)/ Apply the monadic action to every element of the vector, discarding the results.
587+
-- The vector is not modified.
556588
--
557589
-- @since 0.12.3.0
558590
mapM_ :: (PrimMonad m) => (a -> m b) -> MVector (PrimState m) a -> m ()
559591
{-# INLINE mapM_ #-}
560592
mapM_ = G.mapM_
561593

562594
-- | /O(n)/ Apply the monadic action to every element of the vector and its index, discarding the results.
595+
-- The vector is not modified.
563596
--
564597
-- @since 0.12.3.0
565598
imapM_ :: (PrimMonad m) => (Int -> a -> m b) -> MVector (PrimState m) a -> m ()
566599
{-# INLINE imapM_ #-}
567600
imapM_ = G.imapM_
568601

569602
-- | /O(n)/ Apply the monadic action to every element of the vector,
570-
-- discarding the results. It's the same as @flip mapM_@.
603+
-- discarding the results. The vector is not modified.
604+
--
605+
-- It's the same as @flip mapM_@.
571606
--
572607
-- @since 0.12.3.0
573608
forM_ :: (PrimMonad m) => MVector (PrimState m) a -> (a -> m b) -> m ()
574609
{-# INLINE forM_ #-}
575610
forM_ = G.forM_
576611

577612
-- | /O(n)/ Apply the monadic action to every element of the vector
578-
-- and its index, discarding the results. It's the same as @flip imapM_@.
613+
-- and its index, discarding the results. The vector is not modified.
614+
--
615+
-- It's the same as @flip imapM_@.
579616
--
580617
-- @since 0.12.3.0
581618
iforM_ :: (PrimMonad m) => MVector (PrimState m) a -> (Int -> a -> m b) -> m ()

0 commit comments

Comments
 (0)