on master help sign in
Add Foldable and Traversable instances for StreamD and StreamK
977c344Abhiroop Sarkar
src/Streamly/Streams/StreamD.hs+16
@@ -127,6 +127,8 @@ import Prelude
127 notElem, null, head, tail, zipWith)
127 notElem, null, head, tail, zipWith)
128
128
129import Streamly.SVar (MonadAsync, State(..), defState, rstState)
129import Streamly.SVar (MonadAsync, State(..), defState, rstState)
130+
131+import qualified Data.Foldable as F
130import qualified Streamly.Streams.StreamK as K
132import qualified Streamly.Streams.StreamK as K
131
133
132------------------------------------------------------------------------------
134------------------------------------------------------------------------------
@@ -148,6 +150,20 @@ instance Functor (Step s) where
148-- current state, and the current state.
150-- current state, and the current state.
149data Stream m a = forall s. Stream (State K.Stream m a -> s -> m (Step s a)) s
151data Stream m a = forall s. Stream (State K.Stream m a -> s -> m (Step s a)) s
150
152
153+instance (Foldable m, Monad m) => Foldable (Stream m) where
154+ {-# INLINE foldMap #-}
155+ foldMap f = F.fold . toFoldable . map f
156+ where
157+ toFoldable :: (Monad m, Monoid a) => Stream m a -> m a
158+ toFoldable = foldr mappend mempty
159+
160+instance (Traversable m, Monad m) => Traversable (Stream m) where
161+ {-# INLINE traverse #-}
162+ traverse f = sequenceA . fmap f
163+
164+ {-# INLINE sequenceA #-}
165+ sequenceA = traverse id
166+
151------------------------------------------------------------------------------
167------------------------------------------------------------------------------
152-- Construction
168-- Construction
153------------------------------------------------------------------------------
169------------------------------------------------------------------------------
src/Streamly/Streams/StreamK.hs+15
@@ -153,6 +153,7 @@ import Prelude
153 take, filter, all, any, takeWhile, drop, dropWhile, minimum,
153 take, filter, all, any, takeWhile, drop, dropWhile, minimum,
154 maximum, elem, notElem, null, head, tail, init, zipWith, lookup,
154 maximum, elem, notElem, null, head, tail, init, zipWith, lookup,
155 foldr1)
155 foldr1)
156+import qualified Data.Foldable as F
156import qualified Prelude
157import qualified Prelude
157
158
158import Streamly.SVar
159import Streamly.SVar
@@ -188,6 +189,20 @@ newtype Stream m a =
188 -> m r
189 -> m r
189 }
190 }
190
191
192+instance (Foldable m, Monad m) => Foldable (Stream m) where
193+ {-# INLINE foldMap #-}
194+ foldMap f = F.fold . toFoldable . map f
195+ where
196+ toFoldable :: (Monad m, Monoid a) => Stream m a -> m a
197+ toFoldable = foldr mappend mempty
198+
199+instance (Traversable m, Monad m) => Traversable (Stream m) where
200+ {-# INLINE traverse #-}
201+ traverse f = sequenceA . fmap f
202+
203+ {-# INLINE sequenceA #-}
204+ sequenceA = traverse id
205+
191-- XXX make this the default "unStream"
206-- XXX make this the default "unStream"
192-- | unwraps the Stream type producing the stream function that can be run with
207-- | unwraps the Stream type producing the stream function that can be run with
193-- continuations.
208-- continuations.