Commit 2a394246 authored by Richard Lupton's avatar Richard Lupton Committed by Marge Bot

Use Foldable instance of Bag for specialised Bag folds (#16969)

parent 3a1efe1a
...@@ -194,32 +194,26 @@ foldBag t u e (ListBag xs) = foldr (t.u) e xs ...@@ -194,32 +194,26 @@ foldBag t u e (ListBag xs) = foldr (t.u) e xs
foldrBag :: (a -> r -> r) -> r foldrBag :: (a -> r -> r) -> r
-> Bag a -> Bag a
-> r -> r
-- Maintained for backward compatibility - now just a specialisation of
foldrBag _ z EmptyBag = z -- Foldable.
foldrBag k z (UnitBag x) = k x z foldrBag = Foldable.foldr
foldrBag k z (TwoBags b1 b2) = foldrBag k (foldrBag k z b2) b1
foldrBag k z (ListBag xs) = foldr k z xs
foldlBag :: (r -> a -> r) -> r foldlBag :: (r -> a -> r) -> r
-> Bag a -> Bag a
-> r -> r
-- Maintained for backward compatibility - now just a specialisation of
foldlBag _ z EmptyBag = z -- Foldable.
foldlBag k z (UnitBag x) = k z x foldlBag = Foldable.foldl
foldlBag k z (TwoBags b1 b2) = foldlBag k (foldlBag k z b1) b2
foldlBag k z (ListBag xs) = foldl k z xs
foldrBagM :: (Monad m) => (a -> b -> m b) -> b -> Bag a -> m b foldrBagM :: (Monad m) => (a -> b -> m b) -> b -> Bag a -> m b
foldrBagM _ z EmptyBag = return z -- Maintained for backward compatibility - now just a specialisation of
foldrBagM k z (UnitBag x) = k x z -- Foldable.
foldrBagM k z (TwoBags b1 b2) = do { z' <- foldrBagM k z b2; foldrBagM k z' b1 } foldrBagM = Foldable.foldrM
foldrBagM k z (ListBag xs) = foldrM k z xs
foldlBagM :: (Monad m) => (b -> a -> m b) -> b -> Bag a -> m b foldlBagM :: (Monad m) => (b -> a -> m b) -> b -> Bag a -> m b
foldlBagM _ z EmptyBag = return z -- Maintained for backward compatibility - now just a specialisation of
foldlBagM k z (UnitBag x) = k z x -- Foldable.
foldlBagM k z (TwoBags b1 b2) = do { z' <- foldlBagM k z b1; foldlBagM k z' b2 } foldlBagM = Foldable.foldlM
foldlBagM k z (ListBag xs) = foldlM k z xs
mapBag :: (a -> b) -> Bag a -> Bag b mapBag :: (a -> b) -> Bag a -> Bag b
mapBag = fmap mapBag = fmap
...@@ -343,4 +337,17 @@ instance Data a => Data (Bag a) where ...@@ -343,4 +337,17 @@ instance Data a => Data (Bag a) where
dataCast1 x = gcast1 x dataCast1 x = gcast1 x
instance Foldable.Foldable Bag where instance Foldable.Foldable Bag where
foldr = foldrBag foldr _ z EmptyBag = z
foldr k z (UnitBag x) = k x z
foldr k z (TwoBags b1 b2) = foldr k (foldr k z b2) b1
foldr k z (ListBag xs) = foldr k z xs
foldl _ z EmptyBag = z
foldl k z (UnitBag x) = k z x
foldl k z (TwoBags b1 b2) = foldl k (foldl k z b1) b2
foldl k z (ListBag xs) = foldl k z xs
foldl' _ z EmptyBag = z
foldl' k z (UnitBag x) = k z x
foldl' k z (TwoBags b1 b2) = let r1 = foldl' k z b1 in seq r1 $ foldl' k r1 b2
foldl' k z (ListBag xs) = foldl' k z xs
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