Skip to content

Commit 57c4e16

Browse files
committed
Add monoid instances
1 parent e15160f commit 57c4e16

File tree

2 files changed

+66
-21
lines changed

2 files changed

+66
-21
lines changed

src/Data/Foldable.purs

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import Control.Apply
44
import Data.Either
55
import Data.Maybe
66
import Data.Monoid
7+
import Data.Monoid.Additive
8+
import Data.Monoid.Dual
79
import Data.Monoid.First
10+
import Data.Monoid.Last
11+
import Data.Monoid.Multiplicative
812
import Data.Tuple
913

1014
class Foldable f where
@@ -14,38 +18,55 @@ class Foldable f where
1418

1519
instance 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

2224
instance 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

3232
instance 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

4240
instance 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+
4970
fold :: forall f m. (Foldable f, Monoid m) => f m -> m
5071
fold = foldMap id
5172

@@ -99,7 +120,8 @@ find p f = case foldMap (\x -> if p x then [x] else []) f of
99120
lookup :: forall a b f. (Eq a, Foldable f) => a -> f (Tuple a b) -> Maybe b
100121
lookup 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

src/Data/Traversable.purs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import Data.Array (zipWith)
1414
import Data.Either
1515
import Data.Foldable
1616
import Data.Maybe
17+
import Data.Monoid.Additive
18+
import Data.Monoid.Dual
19+
import Data.Monoid.First
20+
import Data.Monoid.Last
21+
import Data.Monoid.Multiplicative
1722
import Data.Tuple
1823

1924
class (Functor t, Foldable t) <= Traversable t where
@@ -23,29 +28,45 @@ class (Functor t, Foldable t) <= Traversable t where
2328
instance traversableArray :: Traversable [] where
2429
traverse _ [] = pure []
2530
traverse f (x:xs) = (:) <$> (f x) <*> traverse f xs
26-
2731
sequence [] = pure []
2832
sequence (x:xs) = (:) <$> x <*> sequence xs
2933

3034
instance traversableEither :: Traversable (Either a) where
3135
traverse _ (Left x) = pure (Left x)
3236
traverse f (Right x) = Right <$> f x
33-
3437
sequence (Left x) = pure (Left x)
3538
sequence (Right x) = Right <$> x
3639

3740
instance traversableMaybe :: Traversable Maybe where
3841
traverse _ Nothing = pure Nothing
3942
traverse f (Just x) = Just <$> f x
40-
4143
sequence Nothing = pure Nothing
4244
sequence (Just x) = Just <$> x
4345

4446
instance traversableTuple :: Traversable (Tuple a) where
4547
traverse f (Tuple x y) = Tuple x <$> f y
46-
4748
sequence (Tuple x y) = Tuple x <$> y
4849

50+
instance traversableAdditive :: Traversable Additive where
51+
traverse f (Additive x) = Additive <$> f x
52+
sequence (Additive x) = Additive <$> x
53+
54+
instance traversableDual :: Traversable Dual where
55+
traverse f (Dual x) = Dual <$> f x
56+
sequence (Dual x) = Dual <$> x
57+
58+
instance traversableFirst :: Traversable First where
59+
traverse f (First x) = First <$> traverse f x
60+
sequence (First x) = First <$> sequence x
61+
62+
instance traversableLast :: Traversable Last where
63+
traverse f (Last x) = Last <$> traverse f x
64+
sequence (Last x) = Last <$> sequence x
65+
66+
instance traversableMultiplicative :: Traversable Multiplicative where
67+
traverse f (Multiplicative x) = Multiplicative <$> f x
68+
sequence (Multiplicative x) = Multiplicative <$> x
69+
4970
for :: forall a b m t. (Applicative m, Traversable t) => t a -> (a -> m b) -> m (t b)
5071
for x f = traverse f x
5172

@@ -97,4 +118,3 @@ scanr f b0 xs = snd $ mapAccumR (\b a -> let b' = f a b in Tuple b' b') b0 xs
97118

98119
mapAccumR :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
99120
mapAccumR f s0 xs = stateR (traverse (\a -> StateR $ \s -> f s a) xs) s0
100-

0 commit comments

Comments
 (0)