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

Treeowl map maybe m #333

Merged
merged 4 commits into from
Oct 12, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions Data/Vector.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ module Data.Vector (
-- * Working with predicates

-- ** Filtering
filter, ifilter, uniq,
mapMaybe, imapMaybe, catMaybes,
filterM,
filter, ifilter, filterM, uniq,
mapMaybe, imapMaybe,
mapMaybeM, imapMaybeM,
catMaybes,
takeWhile, dropWhile,

-- ** Partitioning
Expand Down Expand Up @@ -1298,6 +1299,18 @@ filterM :: Monad m => (a -> m Bool) -> Vector a -> m (Vector a)
{-# INLINE filterM #-}
filterM = G.filterM

-- | /O(n)/ Apply monadic function to each element of vector and
-- discard elements returning Nothing.
mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Vector a -> m (Vector b)
{-# INLINE mapMaybeM #-}
mapMaybeM = G.mapMaybeM

-- | /O(n)/ Apply monadic function to each element of vector and its index.
-- Discards elements returning Nothing.
imapMaybeM :: Monad m => (Int -> a -> m (Maybe b)) -> Vector a -> m (Vector b)
{-# INLINE imapMaybeM #-}
imapMaybeM = G.imapMaybeM

-- | /O(n)/ Yield the longest prefix of elements satisfying the predicate
-- without copying.
takeWhile :: (a -> Bool) -> Vector a -> Vector a
Expand Down
6 changes: 5 additions & 1 deletion Data/Vector/Fusion/Bundle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module Data.Vector.Fusion.Bundle (
fromVector, reVector, fromVectors, concatVectors,

-- * Monadic combinators
mapM, mapM_, zipWithM, zipWithM_, filterM, foldM, fold1M, foldM', fold1M',
mapM, mapM_, zipWithM, zipWithM_, filterM, mapMaybeM, foldM, fold1M, foldM', fold1M',

eq, cmp, eqBy, cmpBy
) where
Expand Down Expand Up @@ -550,6 +550,10 @@ filterM :: Monad m => (a -> m Bool) -> Bundle v a -> M.Bundle m v a
{-# INLINE filterM #-}
filterM f = M.filterM f . lift

mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Bundle v a -> M.Bundle m v b
{-# INLINE mapMaybeM #-}
mapMaybeM f = M.mapMaybeM f . lift

-- | Monadic fold
foldM :: Monad m => (a -> b -> m a) -> a -> Bundle v b -> m a
{-# INLINE foldM #-}
Expand Down
7 changes: 6 additions & 1 deletion Data/Vector/Fusion/Bundle/Monadic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module Data.Vector.Fusion.Bundle.Monadic (
eqBy, cmpBy,

-- * Filtering
filter, filterM, takeWhile, takeWhileM, dropWhile, dropWhileM,
filter, filterM, mapMaybeM, takeWhile, takeWhileM, dropWhile, dropWhileM,

-- * Searching
elem, notElem, find, findM, findIndex, findIndexM,
Expand Down Expand Up @@ -460,6 +460,11 @@ filterM :: Monad m => (a -> m Bool) -> Bundle m v a -> Bundle m v a
{-# INLINE_FUSED filterM #-}
filterM f Bundle{sElems = s, sSize = n} = fromStream (S.filterM f s) (toMax n)

-- | Apply monadic function to each element and drop all Nothings
mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Bundle m v a -> Bundle m v b
{-# INLINE_FUSED mapMaybeM #-}
mapMaybeM f Bundle{sElems = s, sSize = n} = fromStream (S.mapMaybeM f s) (toMax n)

-- | Longest prefix of elements that satisfy the predicate
takeWhile :: Monad m => (a -> Bool) -> Bundle m v a -> Bundle m v a
{-# INLINE takeWhile #-}
Expand Down
19 changes: 18 additions & 1 deletion Data/Vector/Fusion/Stream/Monadic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module Data.Vector.Fusion.Stream.Monadic (
eqBy, cmpBy,

-- * Filtering
filter, filterM, uniq, mapMaybe, catMaybes, takeWhile, takeWhileM, dropWhile, dropWhileM,
filter, filterM, uniq, mapMaybe, mapMaybeM, catMaybes, takeWhile, takeWhileM, dropWhile, dropWhileM,

-- * Searching
elem, notElem, find, findM, findIndex, findIndexM,
Expand Down Expand Up @@ -703,6 +703,23 @@ filterM f (Stream step t) = Stream step' t
Skip s' -> return $ Skip s'
Done -> return $ Done

-- | Apply monadic function to each element and drop all Nothings
mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Stream m a -> Stream m b
{-# INLINE_FUSED mapMaybeM #-}
mapMaybeM f (Stream step t) = Stream step' t
where
{-# INLINE_INNER step' #-}
step' s = do
r <- step s
case r of
Yield x s' -> do
fx <- f x
return $ case fx of
Nothing -> Skip s'
Just b -> Yield b s'
Skip s' -> return $ Skip s'
Done -> return $ Done

-- | Drop repeated adjacent elements.
uniq :: (Eq a, Monad m) => Stream m a -> Stream m a
{-# INLINE_FUSED uniq #-}
Expand Down
17 changes: 15 additions & 2 deletions Data/Vector/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ module Data.Vector.Generic (
-- * Working with predicates

-- ** Filtering
filter, ifilter, uniq,
filter, ifilter, filterM, uniq,
mapMaybe, imapMaybe,
filterM,
mapMaybeM, imapMaybeM,
takeWhile, dropWhile,

-- ** Partitioning
Expand Down Expand Up @@ -1372,6 +1372,19 @@ filterM :: (Monad m, Vector v a) => (a -> m Bool) -> v a -> m (v a)
{-# INLINE filterM #-}
filterM f = unstreamM . Bundle.filterM f . stream

-- | /O(n)/ Apply monadic function to each element of vector and
-- discard elements returning Nothing.
mapMaybeM :: (Monad m, Vector v a, Vector v b) => (a -> m (Maybe b)) -> v a -> m (v b)
{-# INLINE mapMaybeM #-}
mapMaybeM f = unstreamM . Bundle.mapMaybeM f . stream

-- | /O(n)/ Apply monadic function to each element of vector and its index.
-- Discards elements returning Nothing.
imapMaybeM :: (Monad m, Vector v a, Vector v b)
=> (Int -> a -> m (Maybe b)) -> v a -> m (v b)
{-# INLINE imapMaybeM #-}
imapMaybeM f = unstreamM . Bundle.mapMaybeM (\(i, a) -> f i a) . Bundle.indexed . stream
Shimuuar marked this conversation as resolved.
Show resolved Hide resolved

-- | /O(n)/ Yield the longest prefix of elements satisfying the predicate
-- without copying.
takeWhile :: Vector v a => (a -> Bool) -> v a -> v a
Expand Down
20 changes: 18 additions & 2 deletions Data/Vector/Primitive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ module Data.Vector.Primitive (
-- * Working with predicates

-- ** Filtering
filter, ifilter, uniq,
filter, ifilter, filterM, uniq,
mapMaybe, imapMaybe,
filterM,
mapMaybeM, imapMaybeM,
takeWhile, dropWhile,

-- ** Partitioning
Expand Down Expand Up @@ -1028,11 +1028,27 @@ mapMaybe :: (Prim a, Prim b) => (a -> Maybe b) -> Vector a -> Vector b
{-# INLINE mapMaybe #-}
mapMaybe = G.mapMaybe

-- | /O(n)/ Apply monadic function to each element of vector and
-- discard elements returning Nothing.
mapMaybeM
:: (Monad m, Prim a, Prim b)
=> (a -> m (Maybe b)) -> Vector a -> m (Vector b)
{-# INLINE mapMaybeM #-}
mapMaybeM = G.mapMaybeM

-- | /O(n)/ Drop elements when predicate, applied to index and value, returns Nothing
imapMaybe :: (Prim a, Prim b) => (Int -> a -> Maybe b) -> Vector a -> Vector b
{-# INLINE imapMaybe #-}
imapMaybe = G.imapMaybe

-- | /O(n)/ Apply monadic function to each element of vector and its index.
-- Discards elements returning Nothing.
imapMaybeM
:: (Monad m, Prim a, Prim b)
=> (Int -> a -> m (Maybe b)) -> Vector a -> m (Vector b)
{-# INLINE imapMaybeM #-}
imapMaybeM = G.imapMaybeM

-- | /O(n)/ Drop elements that do not satisfy the monadic predicate
filterM :: (Monad m, Prim a) => (a -> m Bool) -> Vector a -> m (Vector a)
{-# INLINE filterM #-}
Expand Down
20 changes: 18 additions & 2 deletions Data/Vector/Storable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ module Data.Vector.Storable (
-- * Working with predicates

-- ** Filtering
filter, ifilter, uniq,
filter, ifilter, filterM, uniq,
mapMaybe, imapMaybe,
filterM,
mapMaybeM, imapMaybeM,
takeWhile, dropWhile,

-- ** Partitioning
Expand Down Expand Up @@ -1040,6 +1040,22 @@ imapMaybe :: (Storable a, Storable b) => (Int -> a -> Maybe b) -> Vector a -> Ve
{-# INLINE imapMaybe #-}
imapMaybe = G.imapMaybe

-- | /O(n)/ Apply monadic function to each element of vector and
-- discard elements returning Nothing.
mapMaybeM
:: (Monad m, Storable a, Storable b)
=> (a -> m (Maybe b)) -> Vector a -> m (Vector b)
{-# INLINE mapMaybeM #-}
mapMaybeM = G.mapMaybeM

-- | /O(n)/ Apply monadic function to each element of vector and its index.
-- Discards elements returning Nothing.
imapMaybeM
:: (Monad m, Storable a, Storable b)
=> (Int -> a -> m (Maybe b)) -> Vector a -> m (Vector b)
{-# INLINE imapMaybeM #-}
imapMaybeM = G.imapMaybeM

-- | /O(n)/ Drop elements that do not satisfy the monadic predicate
filterM :: (Monad m, Storable a) => (a -> m Bool) -> Vector a -> m (Vector a)
{-# INLINE filterM #-}
Expand Down
16 changes: 14 additions & 2 deletions Data/Vector/Unboxed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ module Data.Vector.Unboxed (
-- * Working with predicates

-- ** Filtering
filter, ifilter, uniq,
filter, ifilter, filterM, uniq,
mapMaybe, imapMaybe,
filterM,
mapMaybeM, imapMaybeM,
takeWhile, dropWhile,

-- ** Partitioning
Expand Down Expand Up @@ -1034,6 +1034,18 @@ imapMaybe :: (Unbox a, Unbox b) => (Int -> a -> Maybe b) -> Vector a -> Vector b
{-# INLINE imapMaybe #-}
imapMaybe = G.imapMaybe

-- | /O(n)/ Apply monadic function to each element of vector and
-- discard elements returning Nothing.
mapMaybeM :: (Monad m, Unbox a, Unbox b) => (a -> m (Maybe b)) -> Vector a -> m (Vector b)
{-# INLINE mapMaybeM #-}
mapMaybeM = G.mapMaybeM

-- | /O(n)/ Apply monadic function to each element of vector and its index.
-- Discards elements returning Nothing.
imapMaybeM :: (Monad m, Unbox a, Unbox b) => (Int -> a -> m (Maybe b)) -> Vector a -> m (Vector b)
{-# INLINE imapMaybeM #-}
imapMaybeM = G.imapMaybeM

-- | /O(n)/ Drop elements that do not satisfy the monadic predicate
filterM :: (Monad m, Unbox a) => (a -> m Bool) -> Vector a -> m (Vector a)
{-# INLINE filterM #-}
Expand Down