Skip to content

Commit

Permalink
Use foldr in place of explicit recursion and toList (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephcsible authored and vrom911 committed Sep 15, 2019
1 parent 50ed105 commit d3462a8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
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

0 comments on commit d3462a8

Please sign in to comment.