Ticket #5538: Foldable.hs.diff

File Foldable.hs.diff, 2.9 KB (added by guest, 4 years ago)
  • Data/Foldable.hs

    diff --git a/Data/Foldable.hs b/Data/Foldable.hs
    index 354bd8b..500ce8c 100644
    a b module Data.Foldable ( 
    2222    -- * Folds
    2323    Foldable(..),
    2424    -- ** Special biased folds
    25     foldr',
    26     foldl',
    2725    foldrM,
    2826    foldlM,
    2927    -- ** Folding actions
    import Prelude hiding (foldl, foldr, foldl1, foldr1, mapM_, sequence_, 
    6159                elem, notElem, concat, concatMap, and, or, any, all,
    6260                sum, product, maximum, minimum)
    6361import qualified Prelude (foldl, foldr, foldl1, foldr1)
     62import qualified Data.List as List (foldl')
    6463import Control.Applicative
    6564import Control.Monad (MonadPlus(..))
    6665import Data.Maybe (fromMaybe, listToMaybe)
    class Foldable t where 
    121120    foldr :: (a -> b -> b) -> b -> t a -> b
    122121    foldr f z t = appEndo (foldMap (Endo . f) t) z
    123122
     123    -- | Right-associative fold of a structure,
     124    -- but with strict application of the operator.
     125    foldr' :: (a -> b -> b) -> b -> t a -> b
     126    foldr' f z0 xs = foldl f' id xs z0
     127      where f' k x z = k $! f x z
     128
    124129    -- | Left-associative fold of a structure.
    125130    --
    126131    -- @'foldl' f z = 'Prelude.foldl' f z . 'toList'@
    127132    foldl :: (a -> b -> a) -> a -> t b -> a
    128133    foldl f z t = appEndo (getDual (foldMap (Dual . Endo . flip f) t)) z
    129134
     135    -- | Left-associative fold of a structure.
     136    -- but with strict application of the operator.
     137    --
     138    -- @'foldl' f z = 'List.foldl'' f z . 'toList'@
     139    foldl' :: (a -> b -> a) -> a -> t b -> a
     140    foldl' f z0 xs = foldr f' id xs z0
     141      where f' x k z = k $! f z x
     142
    130143    -- | A variant of 'foldr' that has no base case,
    131144    -- and thus may only be applied to non-empty structures.
    132145    --
    instance Foldable Maybe where 
    161174instance Foldable [] where
    162175    foldr = Prelude.foldr
    163176    foldl = Prelude.foldl
     177    foldl' = List.foldl'
    164178    foldr1 = Prelude.foldr1
    165179    foldl1 = Prelude.foldl1
    166180
    instance Ix i => Foldable (Array i) where 
    170184    foldr1 f = Prelude.foldr1 f . elems
    171185    foldl1 f = Prelude.foldl1 f . elems
    172186
    173 -- | Fold over the elements of a structure,
    174 -- associating to the right, but strictly.
    175 foldr' :: Foldable t => (a -> b -> b) -> b -> t a -> b
    176 foldr' f z0 xs = foldl f' id xs z0
    177   where f' k x z = k $! f x z
    178 
    179187-- | Monadic fold over the elements of a structure,
    180188-- associating to the right, i.e. from right to left.
    181189foldrM :: (Foldable t, Monad m) => (a -> b -> m b) -> b -> t a -> m b
    182190foldrM f z0 xs = foldl f' return xs z0
    183191  where f' k x z = f x z >>= k
    184192
    185 -- | Fold over the elements of a structure,
    186 -- associating to the left, but strictly.
    187 foldl' :: Foldable t => (a -> b -> a) -> a -> t b -> a
    188 foldl' f z0 xs = foldr f' id xs z0
    189   where f' x k z = k $! f z x
    190 
    191193-- | Monadic fold over the elements of a structure,
    192194-- associating to the left, i.e. from left to right.
    193195foldlM :: (Foldable t, Monad m) => (a -> b -> m a) -> a -> t b -> m a