@@ -4,7 +4,11 @@ import Control.Apply
44import Data.Either
55import Data.Maybe
66import Data.Monoid
7+ import Data.Monoid.Additive
8+ import Data.Monoid.Dual
79import Data.Monoid.First
10+ import Data.Monoid.Last
11+ import Data.Monoid.Multiplicative
812import Data.Tuple
913
1014class Foldable f where
@@ -14,38 +18,55 @@ class Foldable f where
1418
1519instance foldableArray :: Foldable [] where
1620 foldr f z xs = foldrArray f z xs
17-
1821 foldl f z xs = foldlArray f z xs
19-
2022 foldMap f xs = foldr (\x acc -> f x <> acc) mempty xs
2123
2224instance foldableEither :: Foldable (Either a ) where
2325 foldr _ z (Left _) = z
2426 foldr f z (Right x) = x `f` z
25-
2627 foldl _ z (Left _) = z
2728 foldl f z (Right x) = z `f` x
28-
2929 foldMap f (Left _) = mempty
3030 foldMap f (Right x) = f x
3131
3232instance foldableMaybe :: Foldable Maybe where
3333 foldr _ z Nothing = z
3434 foldr f z (Just x) = x `f` z
35-
3635 foldl _ z Nothing = z
3736 foldl f z (Just x) = z `f` x
38-
3937 foldMap f Nothing = mempty
4038 foldMap f (Just x) = f x
4139
4240instance foldableTuple :: Foldable (Tuple a ) where
4341 foldr f z (Tuple _ x) = x `f` z
44-
4542 foldl f z (Tuple _ x) = z `f` x
46-
4743 foldMap f (Tuple _ x) = f x
4844
45+ instance foldableAdditive :: Foldable Additive where
46+ foldr f z (Additive x) = x `f` z
47+ foldl f z (Additive x) = z `f` x
48+ foldMap f (Additive x) = f x
49+
50+ instance foldableDual :: Foldable Dual where
51+ foldr f z (Dual x) = x `f` z
52+ foldl f z (Dual x) = z `f` x
53+ foldMap f (Dual x) = f x
54+
55+ instance foldableFirst :: Foldable First where
56+ foldr f z (First x) = foldr f z x
57+ foldl f z (First x) = foldl f z x
58+ foldMap f (First x) = foldMap f x
59+
60+ instance foldableLast :: Foldable Last where
61+ foldr f z (Last x) = foldr f z x
62+ foldl f z (Last x) = foldl f z x
63+ foldMap f (Last x) = foldMap f x
64+
65+ instance foldableMultiplicative :: Foldable Multiplicative where
66+ foldr f z (Multiplicative x) = x `f` z
67+ foldl f z (Multiplicative x) = z `f` x
68+ foldMap f (Multiplicative x) = f x
69+
4970fold :: forall f m . (Foldable f , Monoid m ) => f m -> m
5071fold = foldMap id
5172
@@ -99,7 +120,8 @@ find p f = case foldMap (\x -> if p x then [x] else []) f of
99120lookup :: forall a b f . (Eq a , Foldable f ) => a -> f (Tuple a b ) -> Maybe b
100121lookup a f = runFirst $ foldMap (\(Tuple a' b) -> First (if a == a' then Just b else Nothing )) f
101122
102- foreign import foldrArray " " "
123+ foreign import foldrArray
124+ " " "
103125 function foldrArray(f) {
104126 return function(z) {
105127 return function(xs) {
@@ -108,11 +130,13 @@ foreign import foldrArray """
108130 acc = f(xs[i])(acc);
109131 }
110132 return acc;
111- }
112- }
113- }" " " :: forall a b . (a -> b -> b ) -> b -> [a ] -> b
133+ };
134+ };
135+ }
136+ " " " :: forall a b . (a -> b -> b ) -> b -> [a ] -> b
114137
115- foreign import foldlArray " " "
138+ foreign import foldlArray
139+ " " "
116140 function foldlArray(f) {
117141 return function(z) {
118142 return function(xs) {
@@ -121,6 +145,7 @@ foreign import foldlArray """
121145 acc = f(acc)(xs[i]);
122146 }
123147 return acc;
124- }
125- }
126- }" " " :: forall a b . (b -> a -> b ) -> b -> [a ] -> b
148+ };
149+ };
150+ }
151+ " " " :: forall a b . (b -> a -> b ) -> b -> [a ] -> b
0 commit comments