Permalink
Browse files

Added instances for Foldable and Traversable.

  • Loading branch information...
1 parent 0ba3995 commit 504f5496f717ae0c3ebec2ee095146af4a19ab16 @warrenharris warrenharris committed with Nov 14, 2010
Showing with 16 additions and 0 deletions.
  1. +16 −0 Control/Monad/Stream.hs
@@ -29,6 +29,10 @@ module Control.Monad.Stream ( Stream, suspended, runStream ) where
import Control.Monad
import Control.Applicative
import Control.Monad.Logic
+import Data.Foldable
+import Data.Monoid
+import Data.Traversable
+import Prelude hiding (foldr)
-- |
-- Results of non-deterministic computations of type @Stream a@ can be
@@ -131,3 +135,15 @@ instance MonadLogic Stream where
case s of
Nothing -> mzero
Just (a, _) -> return a
+
+instance Foldable Stream where
+ foldMap _ Nil = mempty
+ foldMap f (Single a) = f a
+ foldMap f (Cons a r) = f a `mappend` foldMap f r
+ foldMap f (Susp i) = foldMap f i
+
+instance Traversable Stream where
+ traverse _ Nil = pure Nil
+ traverse f (Single a) = Single <$> f a
+ traverse f (Cons a r) = Cons <$> f a <*> traverse f r
+ traverse f (Susp i) = Susp <$> traverse f i

0 comments on commit 504f549

Please sign in to comment.