Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement iforM and iforM_ (#262) #311

Merged
merged 1 commit into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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