on master help sign in
Merge pull request #124 from Abhiroop/foldable-traversable
7527247Harendra Kumar
src/Streamly/Streams/Ahead.hs+2−1
@@ -1,5 +1,6 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE ConstraintKinds #-}
2{-# LANGUAGE ConstraintKinds #-}
3+{-# LANGUAGE DeriveTraversable #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving#-}
6{-# LANGUAGE GeneralizedNewtypeDeriving#-}
@@ -619,7 +620,7 @@ consMAhead m r = K.yieldM m `aheadS` r
619--
620--
620-- @since 0.3.0
621-- @since 0.3.0
621newtype AheadT m a = AheadT {getAheadT :: Stream m a}
622newtype AheadT m a = AheadT {getAheadT :: Stream m a}
622− deriving (MonadTrans)
623+ deriving (MonadTrans, Foldable, Traversable)
623
624
624-- | A serial IO stream of elements of type @a@ with concurrent lookahead. See
625-- | A serial IO stream of elements of type @a@ with concurrent lookahead. See
625-- 'AheadT' documentation for more details.
626-- 'AheadT' documentation for more details.
src/Streamly/Streams/Async.hs+3−2
@@ -1,5 +1,6 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE ConstraintKinds #-}
2{-# LANGUAGE ConstraintKinds #-}
3+{-# LANGUAGE DeriveTraversable #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving#-}
6{-# LANGUAGE GeneralizedNewtypeDeriving#-}
@@ -635,7 +636,7 @@ consMAsync m r = K.yieldM m `asyncS` r
635--
636--
636-- @since 0.1.0
637-- @since 0.1.0
637newtype AsyncT m a = AsyncT {getAsyncT :: Stream m a}
638newtype AsyncT m a = AsyncT {getAsyncT :: Stream m a}
638− deriving (MonadTrans)
639+ deriving (MonadTrans, Foldable, Traversable)
639
640
640-- | A demand driven left biased parallely composing IO stream of elements of
641-- | A demand driven left biased parallely composing IO stream of elements of
641-- type @a@. See 'AsyncT' documentation for more details.
642-- type @a@. See 'AsyncT' documentation for more details.
@@ -762,7 +763,7 @@ wAsync m1 m2 = fromStream $ Stream $ \st stp sng yld ->
762--
763--
763-- @since 0.2.0
764-- @since 0.2.0
764newtype WAsyncT m a = WAsyncT {getWAsyncT :: Stream m a}
765newtype WAsyncT m a = WAsyncT {getWAsyncT :: Stream m a}
765− deriving (MonadTrans)
766+ deriving (MonadTrans, Foldable, Traversable)
766
767
767-- | A round robin parallely composing IO stream of elements of type @a@.
768-- | A round robin parallely composing IO stream of elements of type @a@.
768-- See 'WAsyncT' documentation for more details.
769-- See 'WAsyncT' documentation for more details.
src/Streamly/Streams/Parallel.hs+2−1
@@ -1,5 +1,6 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE ConstraintKinds #-}
2{-# LANGUAGE ConstraintKinds #-}
3+{-# LANGUAGE DeriveTraversable #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving#-}
6{-# LANGUAGE GeneralizedNewtypeDeriving#-}
@@ -325,7 +326,7 @@ x |&. f = f |$. x
325--
326--
326-- @since 0.1.0
327-- @since 0.1.0
327newtype ParallelT m a = ParallelT {getParallelT :: Stream m a}
328newtype ParallelT m a = ParallelT {getParallelT :: Stream m a}
328− deriving (MonadTrans)
329+ deriving (MonadTrans, Foldable, Traversable)
329
330
330-- | A parallely composing IO stream of elements of type @a@.
331-- | A parallely composing IO stream of elements of type @a@.
331-- See 'ParallelT' documentation for more details.
332-- See 'ParallelT' documentation for more details.
src/Streamly/Streams/Serial.hs+3−2
@@ -1,5 +1,6 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE ConstraintKinds #-}
2{-# LANGUAGE ConstraintKinds #-}
3+{-# LANGUAGE DeriveTraversable #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving#-}
6{-# LANGUAGE GeneralizedNewtypeDeriving#-}
@@ -121,7 +122,7 @@ import qualified Streamly.Streams.StreamD as D
121--
122--
122-- @since 0.2.0
123-- @since 0.2.0
123newtype SerialT m a = SerialT {getSerialT :: Stream m a}
124newtype SerialT m a = SerialT {getSerialT :: Stream m a}
124− deriving (Semigroup, Monoid, MonadTrans)
125+ deriving (Semigroup, Monoid, MonadTrans, Foldable, Traversable)
125
126
126-- | A serial IO stream of elements of type @a@. See 'SerialT' documentation
127-- | A serial IO stream of elements of type @a@. See 'SerialT' documentation
127-- for more details.
128-- for more details.
@@ -241,7 +242,7 @@ MONAD_COMMON_INSTANCES(SerialT,)
241--
242--
242-- @since 0.2.0
243-- @since 0.2.0
243newtype WSerialT m a = WSerialT {getWSerialT :: Stream m a}
244newtype WSerialT m a = WSerialT {getWSerialT :: Stream m a}
244− deriving (MonadTrans)
245+ deriving (MonadTrans, Foldable, Traversable)
245
246
246-- | An interleaving serial IO stream of elements of type @a@. See 'WSerialT'
247-- | An interleaving serial IO stream of elements of type @a@. See 'WSerialT'
247-- documentation for more details.
248-- documentation for more details.
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------------------------------------------------------------------------------
@@ -149,6 +151,20 @@ instance Functor (Step s) where
149-- current state, and the current state.
151-- current state, and the current state.
150data Stream m a = forall s. Stream (State K.Stream m a -> s -> m (Step s a)) s
152data Stream m a = forall s. Stream (State K.Stream m a -> s -> m (Step s a)) s
151
153
154+instance (Foldable m, Monad m) => Foldable (Stream m) where
155+ {-# INLINE foldMap #-}
156+ foldMap f = F.fold . toFoldable . map f
157+ where
158+ toFoldable :: (Monad m, Monoid a) => Stream m a -> m a
159+ toFoldable = foldr mappend mempty
160+
161+instance (Traversable m, Monad m) => Traversable (Stream m) where
162+ {-# INLINE traverse #-}
163+ traverse f = sequenceA . fmap f
164+
165+ {-# INLINE sequenceA #-}
166+ sequenceA = traverse id
167+
152------------------------------------------------------------------------------
168------------------------------------------------------------------------------
153-- Construction
169-- Construction
154------------------------------------------------------------------------------
170------------------------------------------------------------------------------
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.
src/Streamly/Streams/Zip.hs+3−2
@@ -1,5 +1,6 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE ConstraintKinds #-}
2{-# LANGUAGE ConstraintKinds #-}
3+{-# LANGUAGE DeriveTraversable #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving#-}
6{-# LANGUAGE GeneralizedNewtypeDeriving#-}
@@ -72,7 +73,7 @@ import qualified Streamly.Streams.StreamK as K
72--
73--
73-- @since 0.2.0
74-- @since 0.2.0
74newtype ZipSerialM m a = ZipSerialM {getZipSerialM :: Stream m a}
75newtype ZipSerialM m a = ZipSerialM {getZipSerialM :: Stream m a}
75− deriving (Semigroup, Monoid)
76+ deriving (Semigroup, Monoid, Foldable, Traversable)
76
77
77-- |
78-- |
78-- @since 0.1.0
79-- @since 0.1.0
@@ -166,7 +167,7 @@ zipAsyncWithM f m1 m2 = fromStream $ Stream $ \st stp sng yld -> do
166--
167--
167-- @since 0.2.0
168-- @since 0.2.0
168newtype ZipAsyncM m a = ZipAsyncM {getZipAsyncM :: Stream m a}
169newtype ZipAsyncM m a = ZipAsyncM {getZipAsyncM :: Stream m a}
169− deriving (Semigroup, Monoid)
170+ deriving (Semigroup, Monoid, Foldable, Traversable)
170
171
171-- | An IO stream whose applicative instance zips streams wAsyncly.
172-- | An IO stream whose applicative instance zips streams wAsyncly.
172--
173--