Skip to content

Commit

Permalink
Implement iforM and iforM_ (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
fumieval committed Jun 7, 2020
1 parent 9051022 commit 0793491
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Data/Vector.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ module Data.Vector (

-- ** Monadic mapping
mapM, imapM, mapM_, imapM_, forM, forM_,
iforM, iforM_,

-- ** Zipping
zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
Expand Down Expand Up @@ -1063,6 +1064,18 @@ forM_ :: Monad m => Vector a -> (a -> m b) -> m ()
{-# INLINE forM_ #-}
forM_ = G.forM_

-- | /O(n)/ Apply the monadic action to all elements of the vector and their indices, yielding a
-- vector of results. Equivalent to 'flip' 'imapM'.
iforM :: Monad m => Vector a -> (Int -> a -> m b) -> m (Vector b)
{-# INLINE iforM #-}
iforM = G.iforM

-- | /O(n)/ Apply the monadic action to all elements of the vector and their indices and ignore the
-- results. Equivalent to 'flip' 'imapM_'.
iforM_ :: Monad m => Vector a -> (Int -> a -> m b) -> m ()
{-# INLINE iforM_ #-}
iforM_ = G.iforM_

-- Zipping
-- -------

Expand Down
13 changes: 13 additions & 0 deletions Data/Vector/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ module Data.Vector.Generic (

-- ** Monadic mapping
mapM, imapM, mapM_, imapM_, forM, forM_,
iforM, iforM_,

-- ** Zipping
zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
Expand Down Expand Up @@ -1086,6 +1087,18 @@ forM_ :: (Monad m, Vector v a) => v a -> (a -> m b) -> m ()
{-# INLINE forM_ #-}
forM_ as f = mapM_ f as

-- | /O(n)/ Apply the monadic action to all elements of the vector and their indices, yielding a
-- vector of results. Equivalent to 'flip' 'imapM'.
iforM :: (Monad m, Vector v a, Vector v b) => v a -> (Int -> a -> m b) -> m (v b)
{-# INLINE iforM #-}
iforM as f = imapM f as

-- | /O(n)/ Apply the monadic action to all elements of the vector and their indices and ignore the
-- results. Equivalent to 'flip' 'imapM_'.
iforM_ :: (Monad m, Vector v a) => v a -> (Int -> a -> m b) -> m ()
{-# INLINE iforM_ #-}
iforM_ as f = imapM_ f as

-- Zipping
-- -------

Expand Down
13 changes: 13 additions & 0 deletions Data/Vector/Primitive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ module Data.Vector.Primitive (

-- ** Monadic mapping
mapM, mapM_, forM, forM_,
iforM, iforM_,

-- ** Zipping
zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
Expand Down Expand Up @@ -842,6 +843,18 @@ forM_ :: (Monad m, Prim a) => Vector a -> (a -> m b) -> m ()
{-# INLINE forM_ #-}
forM_ = G.forM_

-- | /O(n)/ Apply the monadic action to all elements of the vector and their indices, yielding a
-- vector of results. Equivalent to 'flip' 'imapM'.
iforM :: (Monad m, Prim a, Prim b) => Vector a -> (Int -> a -> m b) -> m (Vector b)
{-# INLINE iforM #-}
iforM = G.iforM

-- | /O(n)/ Apply the monadic action to all elements of the vector and their indices and ignore the
-- results. Equivalent to 'flip' 'imapM_'.
iforM_ :: (Monad m, Prim a) => Vector a -> (Int -> a -> m b) -> m ()
{-# INLINE iforM_ #-}
iforM_ = G.iforM_

-- Zipping
-- -------

Expand Down
13 changes: 13 additions & 0 deletions Data/Vector/Storable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ module Data.Vector.Storable (

-- ** Monadic mapping
mapM, mapM_, forM, forM_,
iforM, iforM_,

-- ** Zipping
zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
Expand Down Expand Up @@ -869,6 +870,18 @@ forM_ :: (Monad m, Storable a) => Vector a -> (a -> m b) -> m ()
{-# INLINE forM_ #-}
forM_ = G.forM_

-- | /O(n)/ Apply the monadic action to all elements of the vector and their indices, yielding a
-- vector of results. Equivalent to 'flip' 'imapM'.
iforM :: (Monad m, Storable a, Storable b) => Vector a -> (Int -> a -> m b) -> m (Vector b)
{-# INLINE iforM #-}
iforM = G.iforM

-- | /O(n)/ Apply the monadic action to all elements of the vector and their indices and ignore the
-- results. Equivalent to 'flip' 'imapM_'.
iforM_ :: (Monad m, Storable a) => Vector a -> (Int -> a -> m b) -> m ()
{-# INLINE iforM_ #-}
iforM_ = G.iforM_

-- Zipping
-- -------

Expand Down
13 changes: 13 additions & 0 deletions Data/Vector/Unboxed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ module Data.Vector.Unboxed (

-- ** Monadic mapping
mapM, imapM, mapM_, imapM_, forM, forM_,
iforM, iforM_,

-- ** Zipping
zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
Expand Down Expand Up @@ -871,6 +872,18 @@ forM_ :: (Monad m, Unbox a) => Vector a -> (a -> m b) -> m ()
{-# INLINE forM_ #-}
forM_ = G.forM_

-- | /O(n)/ Apply the monadic action to all elements of the vector and their indices, yielding a
-- vector of results. Equivalent to 'flip' 'imapM'.
iforM :: (Monad m, Unbox a, Unbox b) => Vector a -> (Int -> a -> m b) -> m (Vector b)
{-# INLINE iforM #-}
iforM = G.iforM

-- | /O(n)/ Apply the monadic action to all elements of the vector and their indices and ignore the
-- results. Equivalent to 'flip' 'imapM_'.
iforM_ :: (Monad m, Unbox a) => Vector a -> (Int -> a -> m b) -> m ()
{-# INLINE iforM_ #-}
iforM_ = G.iforM_

-- Zipping
-- -------

Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
`Data.Vector.Storable{.Mutable}` to allow this (the onus is on the user
to ensure that no `Storable` invariants are broken when using these
functions).
* Added `iforM` and `iforM_`

# Changes in version 0.12.1.2

Expand Down

0 comments on commit 0793491

Please sign in to comment.