on master help sign in
Merge pull request #124 from Abhiroop/foldable-traversable

Add Foldable and Traversable instances for StreamD and StreamK

commit75272473819b49d91ce901ab399daaf0a411d8a2 authorHarendra Kumar <harendra.kumar@gmail.com> dateFri 16 Nov 2018, 13:59 (8 years ago) parent2228ab0159a0d2
7 files changed+44−8
src/Streamly/Streams/Ahead.hs+2−1 src/Streamly/Streams/Async.hs+3−2 src/Streamly/Streams/Parallel.hs+2−1 src/Streamly/Streams/Serial.hs+3−2 src/Streamly/Streams/StreamD.hs+16 src/Streamly/Streams/StreamK.hs+15 src/Streamly/Streams/Zip.hs+3−2
src/Streamly/Streams/Ahead.hs+2−1
@@ -1,5 +1,6 @@
11{-# LANGUAGE CPP #-}
22{-# LANGUAGE ConstraintKinds #-}
3+{-# LANGUAGE DeriveTraversable #-}
34{-# LANGUAGE FlexibleContexts #-}
45{-# LANGUAGE FlexibleInstances #-}
56{-# LANGUAGE GeneralizedNewtypeDeriving#-}
@@ -619,7 +620,7 @@ consMAhead m r = K.yieldM m `aheadS` r
619620--
620621-- @since 0.3.0
621622newtype AheadT m a = AheadT {getAheadT :: Stream m a}
622− deriving (MonadTrans)
623+ deriving (MonadTrans, Foldable, Traversable)
623624
624625-- | A serial IO stream of elements of type @a@ with concurrent lookahead. See
625626-- 'AheadT' documentation for more details.
src/Streamly/Streams/Async.hs+3−2
@@ -1,5 +1,6 @@
11{-# LANGUAGE CPP #-}
22{-# LANGUAGE ConstraintKinds #-}
3+{-# LANGUAGE DeriveTraversable #-}
34{-# LANGUAGE FlexibleContexts #-}
45{-# LANGUAGE FlexibleInstances #-}
56{-# LANGUAGE GeneralizedNewtypeDeriving#-}
@@ -635,7 +636,7 @@ consMAsync m r = K.yieldM m `asyncS` r
635636--
636637-- @since 0.1.0
637638newtype AsyncT m a = AsyncT {getAsyncT :: Stream m a}
638− deriving (MonadTrans)
639+ deriving (MonadTrans, Foldable, Traversable)
639640
640641-- | A demand driven left biased parallely composing IO stream of elements of
641642-- type @a@. See 'AsyncT' documentation for more details.
@@ -762,7 +763,7 @@ wAsync m1 m2 = fromStream $ Stream $ \st stp sng yld ->
762763--
763764-- @since 0.2.0
764765newtype WAsyncT m a = WAsyncT {getWAsyncT :: Stream m a}
765− deriving (MonadTrans)
766+ deriving (MonadTrans, Foldable, Traversable)
766767
767768-- | A round robin parallely composing IO stream of elements of type @a@.
768769-- See 'WAsyncT' documentation for more details.
src/Streamly/Streams/Parallel.hs+2−1
@@ -1,5 +1,6 @@
11{-# LANGUAGE CPP #-}
22{-# LANGUAGE ConstraintKinds #-}
3+{-# LANGUAGE DeriveTraversable #-}
34{-# LANGUAGE FlexibleContexts #-}
45{-# LANGUAGE FlexibleInstances #-}
56{-# LANGUAGE GeneralizedNewtypeDeriving#-}
@@ -325,7 +326,7 @@ x |&. f = f |$. x
325326--
326327-- @since 0.1.0
327328newtype ParallelT m a = ParallelT {getParallelT :: Stream m a}
328− deriving (MonadTrans)
329+ deriving (MonadTrans, Foldable, Traversable)
329330
330331-- | A parallely composing IO stream of elements of type @a@.
331332-- See 'ParallelT' documentation for more details.
src/Streamly/Streams/Serial.hs+3−2
@@ -1,5 +1,6 @@
11{-# LANGUAGE CPP #-}
22{-# LANGUAGE ConstraintKinds #-}
3+{-# LANGUAGE DeriveTraversable #-}
34{-# LANGUAGE FlexibleContexts #-}
45{-# LANGUAGE FlexibleInstances #-}
56{-# LANGUAGE GeneralizedNewtypeDeriving#-}
@@ -121,7 +122,7 @@ import qualified Streamly.Streams.StreamD as D
121122--
122123-- @since 0.2.0
123124newtype SerialT m a = SerialT {getSerialT :: Stream m a}
124− deriving (Semigroup, Monoid, MonadTrans)
125+ deriving (Semigroup, Monoid, MonadTrans, Foldable, Traversable)
125126
126127-- | A serial IO stream of elements of type @a@. See 'SerialT' documentation
127128-- for more details.
@@ -241,7 +242,7 @@ MONAD_COMMON_INSTANCES(SerialT,)
241242--
242243-- @since 0.2.0
243244newtype WSerialT m a = WSerialT {getWSerialT :: Stream m a}
244− deriving (MonadTrans)
245+ deriving (MonadTrans, Foldable, Traversable)
245246
246247-- | An interleaving serial IO stream of elements of type @a@. See 'WSerialT'
247248-- documentation for more details.
src/Streamly/Streams/StreamD.hs+16
@@ -127,6 +127,8 @@ import Prelude
127127 notElem, null, head, tail, zipWith)
128128
129129import Streamly.SVar (MonadAsync, State(..), defState, rstState)
130+
131+import qualified Data.Foldable as F
130132import qualified Streamly.Streams.StreamK as K
131133
132134------------------------------------------------------------------------------
@@ -149,6 +151,20 @@ instance Functor (Step s) where
149151-- current state, and the current state.
150152data Stream m a = forall s. Stream (State K.Stream m a -> s -> m (Step s a)) s
151153
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+
152168------------------------------------------------------------------------------
153169-- Construction
154170------------------------------------------------------------------------------
src/Streamly/Streams/StreamK.hs+15
@@ -153,6 +153,7 @@ import Prelude
153153 take, filter, all, any, takeWhile, drop, dropWhile, minimum,
154154 maximum, elem, notElem, null, head, tail, init, zipWith, lookup,
155155 foldr1)
156+import qualified Data.Foldable as F
156157import qualified Prelude
157158
158159import Streamly.SVar
@@ -188,6 +189,20 @@ newtype Stream m a =
188189 -> m r
189190 }
190191
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+
191206-- XXX make this the default "unStream"
192207-- | unwraps the Stream type producing the stream function that can be run with
193208-- continuations.
src/Streamly/Streams/Zip.hs+3−2
@@ -1,5 +1,6 @@
11{-# LANGUAGE CPP #-}
22{-# LANGUAGE ConstraintKinds #-}
3+{-# LANGUAGE DeriveTraversable #-}
34{-# LANGUAGE FlexibleContexts #-}
45{-# LANGUAGE FlexibleInstances #-}
56{-# LANGUAGE GeneralizedNewtypeDeriving#-}
@@ -72,7 +73,7 @@ import qualified Streamly.Streams.StreamK as K
7273--
7374-- @since 0.2.0
7475newtype ZipSerialM m a = ZipSerialM {getZipSerialM :: Stream m a}
75− deriving (Semigroup, Monoid)
76+ deriving (Semigroup, Monoid, Foldable, Traversable)
7677
7778-- |
7879-- @since 0.1.0
@@ -166,7 +167,7 @@ zipAsyncWithM f m1 m2 = fromStream $ Stream $ \st stp sng yld -> do
166167--
167168-- @since 0.2.0
168169newtype ZipAsyncM m a = ZipAsyncM {getZipAsyncM :: Stream m a}
169− deriving (Semigroup, Monoid)
170+ deriving (Semigroup, Monoid, Foldable, Traversable)
170171
171172-- | An IO stream whose applicative instance zips streams wAsyncly.
172173--