diff options
author | Herbert Valerio Riedel <hvr@gnu.org> | 2014-09-28 13:02:53 +0200 |
---|---|---|
committer | Herbert Valerio Riedel <hvr@gnu.org> | 2014-09-28 15:06:39 +0200 |
commit | e5cca4ab246ca2d1ecdd7c39eefd3157547cb6aa (patch) | |
tree | 5fe848bcd68c5a94b3af11b2282df23d759ca823 /libraries/base/Data/Foldable.hs | |
parent | bf3329104c971c84ab178f3ded88254b9594f9cc (diff) | |
download | haskell-e5cca4ab246ca2d1ecdd7c39eefd3157547cb6aa.tar.gz |
Extend `Foldable` class with `length` and `null` methods
This completes the `Foldable` class by two important operations which
this way can be optimised for the underlying structure more easily.
A minor fix for the `containers` submodule was needed to due name clash
Addresses #9621
Reviewed By: ekmett, dfeuer, austin
Differential Revision: https://phabricator.haskell.org/D250
Diffstat (limited to 'libraries/base/Data/Foldable.hs')
-rw-r--r-- | libraries/base/Data/Foldable.hs | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/libraries/base/Data/Foldable.hs b/libraries/base/Data/Foldable.hs index 688fd06ec0..d8310ca49e 100644 --- a/libraries/base/Data/Foldable.hs +++ b/libraries/base/Data/Foldable.hs @@ -149,6 +149,14 @@ class Foldable t where {-# INLINE toList #-} toList t = build (\ c n -> foldr c n t) + -- | Test whether the structure is empty. + null :: Foldable t => t a -> Bool + null = foldr (\_ _ -> False) True + + -- | Returns the size/length of a finite structure as an 'Int'. + length :: Foldable t => t a -> Int + length = foldl' (\c _ -> c+1) 0 + -- | Does the element occur in the structure? elem :: (Foldable t, Eq a) => a -> t a -> Bool elem = any . (==) @@ -186,8 +194,10 @@ instance Foldable [] where foldl1 = List.foldl1 foldr = List.foldr foldr1 = List.foldr1 + length = List.length maximum = List.maximum minimum = List.minimum + null = List.null product = List.product sum = List.sum toList = id |