Commit 6dd218e5 authored by David Feuer's avatar David Feuer Committed by Herbert Valerio Riedel

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
parent 1c0b7362
......@@ -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]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment