Skip to content

Commit

Permalink
add Foldable and Traversable instances for Either a and (,) a
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Paterson committed May 31, 2013
1 parent 216d590 commit a9a9ce6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Data/Foldable.hs
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Data/Traversable.hs
Expand Up @@ -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)

Expand Down

0 comments on commit a9a9ce6

Please sign in to comment.