diff --git a/Data/Foldable.hs b/Data/Foldable.hs
index 354bd8b..500ce8c 100644
|
a
|
b
|
|
| 22 | 22 | -- * Folds |
| 23 | 23 | Foldable(..), |
| 24 | 24 | -- ** Special biased folds |
| 25 | | foldr', |
| 26 | | foldl', |
| 27 | 25 | foldrM, |
| 28 | 26 | foldlM, |
| 29 | 27 | -- ** Folding actions |
| … |
… |
|
| 61 | 59 | elem, notElem, concat, concatMap, and, or, any, all, |
| 62 | 60 | sum, product, maximum, minimum) |
| 63 | 61 | import qualified Prelude (foldl, foldr, foldl1, foldr1) |
| | 62 | import qualified Data.List as List (foldl') |
| 64 | 63 | import Control.Applicative |
| 65 | 64 | import Control.Monad (MonadPlus(..)) |
| 66 | 65 | import Data.Maybe (fromMaybe, listToMaybe) |
| … |
… |
|
| 121 | 120 | foldr :: (a -> b -> b) -> b -> t a -> b |
| 122 | 121 | foldr f z t = appEndo (foldMap (Endo . f) t) z |
| 123 | 122 | |
| | 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 | |
| 124 | 129 | -- | Left-associative fold of a structure. |
| 125 | 130 | -- |
| 126 | 131 | -- @'foldl' f z = 'Prelude.foldl' f z . 'toList'@ |
| 127 | 132 | foldl :: (a -> b -> a) -> a -> t b -> a |
| 128 | 133 | foldl f z t = appEndo (getDual (foldMap (Dual . Endo . flip f) t)) z |
| 129 | 134 | |
| | 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 | |
| 130 | 143 | -- | A variant of 'foldr' that has no base case, |
| 131 | 144 | -- and thus may only be applied to non-empty structures. |
| 132 | 145 | -- |
| … |
… |
|
| 161 | 174 | instance Foldable [] where |
| 162 | 175 | foldr = Prelude.foldr |
| 163 | 176 | foldl = Prelude.foldl |
| | 177 | foldl' = List.foldl' |
| 164 | 178 | foldr1 = Prelude.foldr1 |
| 165 | 179 | foldl1 = Prelude.foldl1 |
| 166 | 180 | |
| … |
… |
|
| 170 | 184 | foldr1 f = Prelude.foldr1 f . elems |
| 171 | 185 | foldl1 f = Prelude.foldl1 f . elems |
| 172 | 186 | |
| 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 | | |
| 179 | 187 | -- | Monadic fold over the elements of a structure, |
| 180 | 188 | -- associating to the right, i.e. from right to left. |
| 181 | 189 | foldrM :: (Foldable t, Monad m) => (a -> b -> m b) -> b -> t a -> m b |
| 182 | 190 | foldrM f z0 xs = foldl f' return xs z0 |
| 183 | 191 | where f' k x z = f x z >>= k |
| 184 | 192 | |
| 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 | | |
| 191 | 193 | -- | Monadic fold over the elements of a structure, |
| 192 | 194 | -- associating to the left, i.e. from left to right. |
| 193 | 195 | foldlM :: (Foldable t, Monad m) => (a -> b -> m a) -> a -> t b -> m a |