[commit: ghc] master: Make Foldable's foldr1 and foldl1 defaults lazier (6dd218e)

git at git.haskell.org git at git.haskell.org
Tue Nov 4 09:07:04 UTC 2014


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/6dd218e5f82f19523f9e7aa5371f5bd7beb19663/ghc

>---------------------------------------------------------------

commit 6dd218e5f82f19523f9e7aa5371f5bd7beb19663
Author: David Feuer <David.Feuer at gmail.com>
Date:   Tue Nov 4 10:03:12 2014 +0100

    Make Foldable's foldr1 and foldl1 defaults lazier
    
    Fixes #9742. Previously, `foldr1` as applied to a list-like
    structure would be strict in the spine, and `foldl1` would
    be strict in the spine of a snoc-list.
    
    See also
    
     https://www.haskell.org/pipermail/libraries/2014-October/024035.html
    
    Differential Revision: https://phabricator.haskell.org/D423


>---------------------------------------------------------------

6dd218e5f82f19523f9e7aa5371f5bd7beb19663
 libraries/base/Data/Foldable.hs | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/libraries/base/Data/Foldable.hs b/libraries/base/Data/Foldable.hs
index 75460bb..a855090 100644
--- a/libraries/base/Data/Foldable.hs
+++ b/libraries/base/Data/Foldable.hs
@@ -130,8 +130,9 @@ class Foldable t where
     foldr1 f xs = fromMaybe (error "foldr1: empty structure")
                     (foldr mf Nothing xs)
       where
-        mf x Nothing = Just x
-        mf x (Just y) = Just (f x y)
+        mf x m = Just (case m of
+                         Nothing -> x
+                         Just y  -> f x y)
 
     -- | A variant of 'foldl' that has no base case,
     -- and thus may only be applied to non-empty structures.
@@ -141,8 +142,9 @@ class Foldable t where
     foldl1 f xs = fromMaybe (error "foldl1: empty structure")
                     (foldl mf Nothing xs)
       where
-        mf Nothing y = Just y
-        mf (Just x) y = Just (f x y)
+        mf m y = Just (case m of
+                         Nothing -> y
+                         Just x  -> f x y)
 
     -- | List of elements of a structure.
     toList :: Foldable t => t a -> [a]



More information about the ghc-commits mailing list