[commit: base] master: add Foldable and Traversable instances for Either a and (, ) a (a9a9ce6)
Ross Paterson
ross at soi.city.ac.uk
Fri May 31 03:29:15 CEST 2013
Repository : ssh://darcs.haskell.org//srv/darcs/packages/base
On branch : master
https://github.com/ghc/packages-base/commit/a9a9ce679e0c57437a17824cd6366d085248d217
>---------------------------------------------------------------
commit a9a9ce679e0c57437a17824cd6366d085248d217
Author: Ross Paterson <ross at soi.city.ac.uk>
Date: Fri May 31 02:25:57 2013 +0100
add Foldable and Traversable instances for Either a and (,) a
Agreed in January 2011, but not implemented:
http://thread.gmane.org/gmane.comp.lang.haskell.libraries/15196
http://thread.gmane.org/gmane.comp.lang.haskell.libraries/17686
http://thread.gmane.org/gmane.comp.lang.haskell.libraries/19594
>---------------------------------------------------------------
Data/Foldable.hs | 12 ++++++++++++
Data/Traversable.hs | 7 +++++++
2 files changed, 19 insertions(+), 0 deletions(-)
diff --git a/Data/Foldable.hs b/Data/Foldable.hs
index 86c8f10..7f77a4a 100644
--- a/Data/Foldable.hs
+++ b/Data/Foldable.hs
@@ -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
diff --git a/Data/Traversable.hs b/Data/Traversable.hs
index f5ac060..be5fe24 100644
--- a/Data/Traversable.hs
+++ b/Data/Traversable.hs
@@ -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)
More information about the ghc-commits
mailing list