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

Use foldr in place of explicit recursion and toList #162

Merged
merged 2 commits into from
Sep 15, 2019
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ The changelog is available [on GitHub][2].
Made `die` be polymorphic in its return type.
* [#164](https://github.com/kowainik/relude/issues/164):
Reexport `ShortByteString`, `toShort`/`fromShort` functions.
* [#162](https://github.com/kowainik/relude/pull/162):
Use `foldr` instead of explicit recursion and `toList`.

## 0.5.0 — Mar 18, 2019

Expand Down
28 changes: 12 additions & 16 deletions src/Relude/Foldable/Fold.hs
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,11 @@ Nothing
False
-}
andM :: (Foldable f, Monad m) => f (m Bool) -> m Bool
andM = go . toList
andM = foldr go (pure True)
where
go [] = pure True
go (p:ps) = do
go p acc = do
q <- p
if q then go ps else pure False
if q then acc else pure False
{-# INLINEABLE andM #-}
{-# SPECIALIZE andM :: [IO Bool] -> IO Bool #-}

Expand All @@ -196,12 +195,11 @@ Just True
Nothing
-}
orM :: (Foldable f, Monad m) => f (m Bool) -> m Bool
orM = go . toList
orM = foldr go (pure False)
where
go [] = pure False
go (p:ps) = do
go p acc = do
q <- p
if q then pure True else go ps
if q then pure True else acc
{-# INLINEABLE orM #-}
{-# SPECIALIZE orM :: [IO Bool] -> IO Bool #-}

Expand All @@ -215,12 +213,11 @@ Just False
Nothing
-}
allM :: (Foldable f, Monad m) => (a -> m Bool) -> f a -> m Bool
allM p = go . toList
allM p = foldr go (pure True)
where
go [] = pure True
go (x:xs) = do
go x acc = do
q <- p x
if q then go xs else pure False
if q then acc else pure False
{-# INLINEABLE allM #-}
{-# SPECIALIZE allM :: (a -> IO Bool) -> [a] -> IO Bool #-}

Expand All @@ -234,12 +231,11 @@ Just True
Nothing
-}
anyM :: (Foldable f, Monad m) => (a -> m Bool) -> f a -> m Bool
anyM p = go . toList
anyM p = foldr go (pure False)
where
go [] = pure False
go (x:xs) = do
go x acc = do
q <- p x
if q then pure True else go xs
if q then pure True else acc
{-# INLINEABLE anyM #-}
{-# SPECIALIZE anyM :: (a -> IO Bool) -> [a] -> IO Bool #-}

Expand Down