Skip to content

Commit 295e250

Browse files
authored
Add iconcatMap (#531)
1 parent 04194a0 commit 295e250

File tree

8 files changed

+60
-7
lines changed

8 files changed

+60
-7
lines changed

vector/src/Data/Vector.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ module Data.Vector (
9494
indexed,
9595

9696
-- ** Mapping
97-
map, imap, concatMap,
97+
map, imap, concatMap, iconcatMap,
9898

9999
-- ** Monadic mapping
100100
mapM, imapM, mapM_, imapM_, forM, forM_,
@@ -1096,6 +1096,13 @@ concatMap :: (a -> Vector b) -> Vector a -> Vector b
10961096
{-# INLINE concatMap #-}
10971097
concatMap = G.concatMap
10981098

1099+
-- | Map a function to every element of a vector and its index, and concatenate the results.
1100+
--
1101+
-- @since 0.13.3.0
1102+
iconcatMap :: (Int -> a -> Vector b) -> Vector a -> Vector b
1103+
{-# INLINE iconcatMap #-}
1104+
iconcatMap = G.iconcatMap
1105+
10991106
-- Monadic mapping
11001107
-- ---------------
11011108

vector/src/Data/Vector/Generic.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ module Data.Vector.Generic (
8484
indexed,
8585

8686
-- ** Mapping
87-
map, imap, concatMap,
87+
map, imap, concatMap, iconcatMap,
8888

8989
-- ** Monadic mapping
9090
mapM, imapM, mapM_, imapM_, forM, forM_,
@@ -1112,6 +1112,16 @@ concatMap f = unstream
11121112
. Bundle.map f
11131113
. stream
11141114

1115+
-- | Apply a function to every element of a vector and its index, and concatenate the results.
1116+
--
1117+
-- @since 0.13.3.0
1118+
iconcatMap :: (Vector v a, Vector v b) => (Int -> a -> v b) -> v a -> v b
1119+
{-# INLINE iconcatMap #-}
1120+
iconcatMap f = unstream
1121+
. Bundle.concatVectors
1122+
. Bundle.inplace (S.map (uncurry f) . S.indexed) id
1123+
. stream
1124+
11151125
-- Monadic mapping
11161126
-- ---------------
11171127

vector/src/Data/Vector/Primitive.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ module Data.Vector.Primitive (
8484
-- * Elementwise operations
8585

8686
-- ** Mapping
87-
map, imap, concatMap,
87+
map, imap, concatMap, iconcatMap,
8888

8989
-- ** Monadic mapping
9090
mapM, imapM, mapM_, imapM_, forM, forM_,
@@ -882,6 +882,13 @@ concatMap :: (Prim a, Prim b) => (a -> Vector b) -> Vector a -> Vector b
882882
{-# INLINE concatMap #-}
883883
concatMap = G.concatMap
884884

885+
-- | Apply a function to every element of a vector and its index, and concatenate the results.
886+
--
887+
-- @since 0.13.3.0
888+
iconcatMap :: (Prim a, Prim b) => (Int -> a -> Vector b) -> Vector a -> Vector b
889+
{-# INLINE iconcatMap #-}
890+
iconcatMap = G.iconcatMap
891+
885892
-- Monadic mapping
886893
-- ---------------
887894

vector/src/Data/Vector/Storable.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ module Data.Vector.Storable (
8181
-- * Elementwise operations
8282

8383
-- ** Mapping
84-
map, imap, concatMap,
84+
map, imap, concatMap, iconcatMap,
8585

8686
-- ** Monadic mapping
8787
mapM, imapM, mapM_, imapM_, forM, forM_,
@@ -893,6 +893,13 @@ concatMap :: (Storable a, Storable b) => (a -> Vector b) -> Vector a -> Vector b
893893
{-# INLINE concatMap #-}
894894
concatMap = G.concatMap
895895

896+
-- | Apply a function to every element of a vector and its index, and concatenate the results.
897+
--
898+
-- @since 0.13.3.0
899+
iconcatMap :: (Storable a, Storable b) => (Int -> a -> Vector b) -> Vector a -> Vector b
900+
{-# INLINE iconcatMap #-}
901+
iconcatMap = G.iconcatMap
902+
896903
-- Monadic mapping
897904
-- ---------------
898905

vector/src/Data/Vector/Strict.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module Data.Vector.Strict (
9292
indexed,
9393

9494
-- ** Mapping
95-
map, imap, concatMap,
95+
map, imap, concatMap, iconcatMap,
9696

9797
-- ** Monadic mapping
9898
mapM, imapM, mapM_, imapM_, forM, forM_,
@@ -1175,6 +1175,13 @@ concatMap :: (a -> Vector b) -> Vector a -> Vector b
11751175
{-# INLINE concatMap #-}
11761176
concatMap = G.concatMap
11771177

1178+
-- | Apply a function to every element of a vector and its index, and concatenate the results.
1179+
--
1180+
-- @since 0.13.3.0
1181+
iconcatMap :: (Int -> a -> Vector b) -> Vector a -> Vector b
1182+
{-# INLINE iconcatMap #-}
1183+
iconcatMap = G.iconcatMap
1184+
11781185
-- Monadic mapping
11791186
-- ---------------
11801187

vector/src/Data/Vector/Unboxed.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ module Data.Vector.Unboxed (
132132
indexed,
133133

134134
-- ** Mapping
135-
map, imap, concatMap,
135+
map, imap, concatMap, iconcatMap,
136136

137137
-- ** Monadic mapping
138138
mapM, imapM, mapM_, imapM_, forM, forM_,
@@ -931,6 +931,13 @@ concatMap :: (Unbox a, Unbox b) => (a -> Vector b) -> Vector a -> Vector b
931931
{-# INLINE concatMap #-}
932932
concatMap = G.concatMap
933933

934+
-- | Apply a function to every element of a vector and its index, and concatenate the results.
935+
--
936+
-- @since 0.13.3.0
937+
iconcatMap :: (Unbox a, Unbox b) => (Int -> a -> Vector b) -> Vector a -> Vector b
938+
{-# INLINE iconcatMap #-}
939+
iconcatMap = G.iconcatMap
940+
934941
-- Monadic mapping
935942
-- ---------------
936943

vector/tests/Tests/Vector/Property.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ testPolymorphicFunctions _ = $(testProperties [
147147
{- 'prop_unsafeBackpermute, -}
148148

149149
-- Mapping
150-
'prop_map, 'prop_imap, 'prop_concatMap,
150+
'prop_map, 'prop_imap, 'prop_concatMap, 'prop_iconcatMap,
151151

152152
-- Monadic mapping
153153
'prop_mapM, 'prop_mapM_, 'prop_forM, 'prop_forM_,
@@ -435,6 +435,11 @@ testPolymorphicFunctions _ = $(testProperties [
435435
where
436436
prop :: P ((a -> v a) -> v a -> v a) = V.concatMap `eq` concatMap
437437

438+
prop_iconcatMap = forAll arbitrary $ \xs ->
439+
forAll (sized (\n -> resize (n `div` V.length xs) arbitrary)) $ \f -> unP prop f xs
440+
where
441+
prop :: P ((Int -> a -> v a) -> v a -> v a) = V.iconcatMap `eq` iconcatMap
442+
438443
prop_uniq :: P (v a -> v a)
439444
= V.uniq `eq` (map head . group)
440445

vector/tests/Utilities.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ imapM = withIndexFirst mapM
286286
imapM_ :: Monad m => (Int -> a -> m b) -> [a] -> m ()
287287
imapM_ = withIndexFirst mapM_
288288

289+
iconcatMap :: (Int -> a -> [a]) -> [a] -> [a]
290+
iconcatMap f = concat . withIndexFirst map f
291+
289292
izipWith :: (Int -> a -> a -> a) -> [a] -> [a] -> [a]
290293
izipWith = withIndexFirst zipWith
291294

0 commit comments

Comments
 (0)