Permalink
Browse files

add Foldable and Traversable instances for Either a and (,) a

  • Loading branch information...
1 parent 216d590 commit a9a9ce679e0c57437a17824cd6366d085248d217 Ross Paterson committed May 31, 2013
Showing with 19 additions and 0 deletions.
  1. +12 −0 Data/Foldable.hs
  2. +7 −0 Data/Traversable.hs
View
@@ -175,6 +175,18 @@ instance Foldable [] where
foldr1 = Prelude.foldr1
foldl1 = Prelude.foldl1
+instance Foldable (Either a) where
+ foldMap _ (Left _) = mempty
+ foldMap f (Right y) = f y
+
+ foldr _ z (Left _) = z
+ foldr f z (Right y) = f y z
+
+instance Foldable ((,) a) where
+ foldMap f (_, y) = f y
+
+ foldr f z (_, y) = f y z
+
instance Ix i => Foldable (Array i) where
foldr f z = Prelude.foldr f z . elems
foldl f z = Prelude.foldl f z . elems
View
@@ -182,6 +182,13 @@ instance Traversable [] where
mapM = Prelude.mapM
+instance Traversable (Either a) where
+ traverse _ (Left x) = pure (Left x)
+ traverse f (Right y) = Right <$> f y
+
+instance Traversable ((,) a) where
+ traverse f (x, y) = (,) x <$> f y
+
instance Ix i => Traversable (Array i) where
traverse f arr = listArray (bounds arr) `fmap` traverse f (elems arr)

0 comments on commit a9a9ce6

Please sign in to comment.