on master help sign in
Add cons and consM to the IsStream class
0a07ae9Harendra Kumar
src/Streamly/Streams.hs+39−6
@@ -31,9 +31,7 @@ module Streamly.Streams
31
31
32 -- * Construction
32 -- * Construction
33 , nil
33 , nil
34− , cons
35 , (.:)
34 , (.:)
36− , consM
37 , (|:)
35 , (|:)
38 , streamBuild
36 , streamBuild
39 , fromCallback
37 , fromCallback
@@ -134,6 +132,8 @@ import qualified Streamly.Core as S
134class IsStream t where
132class IsStream t where
135 toStream :: t m a -> S.Stream m a
133 toStream :: t m a -> S.Stream m a
136 fromStream :: S.Stream m a -> t m a
134 fromStream :: S.Stream m a -> t m a
135+ cons :: a -> t m a -> t m a
136+ consM :: Monad m => m a -> t m a -> t m a
137
137
138-- | Same as 'IsStream'.
138-- | Same as 'IsStream'.
139--
139--
@@ -169,8 +169,9 @@ infixr 5 `consM`
169-- @
169-- @
170--
170--
171-- @since 0.2.0
171-- @since 0.2.0
172+-- {-# INLINE consMSerial #-}
172−consM :: (IsStream t, Monad m) => m a -> t m a -> t m a
173+consMSerial :: (IsStream t, Monad m) => m a -> t m a -> t m a
173−consM m r = fromStream $ S.consM m (toStream r)
174+consMSerial m r = fromStream $ S.consM m (toStream r)
174
175
175infixr 5 |:
176infixr 5 |:
176
177
@@ -198,8 +199,8 @@ infixr 5 `cons`
198-- @
199-- @
199--
200--
200-- @since 0.1.0
201-- @since 0.1.0
201−cons :: IsStream t => a -> t m a -> t m a
202+consSerial :: IsStream t => a -> t m a -> t m a
202−cons a r = fromStream $ S.cons a (toStream r)
203+consSerial a r = fromStream $ S.cons a (toStream r)
203
204
204infixr 5 .:
205infixr 5 .:
205
206
@@ -411,6 +412,10 @@ type StreamT = SerialT
411instance IsStream SerialT where
412instance IsStream SerialT where
412 toStream = getSerialT
413 toStream = getSerialT
413 fromStream = SerialT
414 fromStream = SerialT
415+ cons = consSerial
416+ {-# INLINE consM #-}
417+ {-# SPECIALIZE consM :: IO a -> SerialT IO a -> SerialT IO a #-}
418+ consM = consMSerial
414
419
415------------------------------------------------------------------------------
420------------------------------------------------------------------------------
416-- Semigroup
421-- Semigroup
@@ -496,6 +501,10 @@ type InterleavedT = WSerialT
496instance IsStream WSerialT where
501instance IsStream WSerialT where
497 toStream = getWSerialT
502 toStream = getWSerialT
498 fromStream = WSerialT
503 fromStream = WSerialT
504+ cons = consSerial
505+ {-# INLINE consM #-}
506+ {-# SPECIALIZE consM :: IO a -> WSerialT IO a -> WSerialT IO a #-}
507+ consM = consMSerial
499
508
500------------------------------------------------------------------------------
509------------------------------------------------------------------------------
501-- Semigroup
510-- Semigroup
@@ -603,6 +612,10 @@ newtype AheadT m a = AheadT {getAheadT :: S.Stream m a}
603instance IsStream AheadT where
612instance IsStream AheadT where
604 toStream = getAheadT
613 toStream = getAheadT
605 fromStream = AheadT
614 fromStream = AheadT
615+ cons = consSerial
616+ {-# INLINE consM #-}
617+ {-# SPECIALIZE consM :: IO a -> AheadT IO a -> AheadT IO a #-}
618+ consM = consMSerial
606
619
607------------------------------------------------------------------------------
620------------------------------------------------------------------------------
608-- Semigroup
621-- Semigroup
@@ -716,6 +729,10 @@ newtype AsyncT m a = AsyncT {getAsyncT :: S.Stream m a}
716instance IsStream AsyncT where
729instance IsStream AsyncT where
717 toStream = getAsyncT
730 toStream = getAsyncT
718 fromStream = AsyncT
731 fromStream = AsyncT
732+ cons = consSerial
733+ {-# INLINE consM #-}
734+ {-# SPECIALIZE consM :: IO a -> AsyncT IO a -> AsyncT IO a #-}
735+ consM = consMSerial
719
736
720------------------------------------------------------------------------------
737------------------------------------------------------------------------------
721-- Semigroup
738-- Semigroup
@@ -836,6 +853,10 @@ newtype WAsyncT m a = WAsyncT {getWAsyncT :: S.Stream m a}
836instance IsStream WAsyncT where
853instance IsStream WAsyncT where
837 toStream = getWAsyncT
854 toStream = getWAsyncT
838 fromStream = WAsyncT
855 fromStream = WAsyncT
856+ cons = consSerial
857+ {-# INLINE consM #-}
858+ {-# SPECIALIZE consM :: IO a -> WAsyncT IO a -> WAsyncT IO a #-}
859+ consM = consMSerial
839
860
840------------------------------------------------------------------------------
861------------------------------------------------------------------------------
841-- Semigroup
862-- Semigroup
@@ -971,6 +992,10 @@ newtype ParallelT m a = ParallelT {getParallelT :: S.Stream m a}
971instance IsStream ParallelT where
992instance IsStream ParallelT where
972 toStream = getParallelT
993 toStream = getParallelT
973 fromStream = ParallelT
994 fromStream = ParallelT
995+ cons = consSerial
996+ {-# INLINE consM #-}
997+ {-# SPECIALIZE consM :: IO a -> ParallelT IO a -> ParallelT IO a #-}
998+ consM = consMSerial
974
999
975------------------------------------------------------------------------------
1000------------------------------------------------------------------------------
976-- Semigroup
1001-- Semigroup
@@ -1043,6 +1068,10 @@ type ZipStream = ZipSerialM
1043instance IsStream ZipSerialM where
1068instance IsStream ZipSerialM where
1044 toStream = getZipSerialM
1069 toStream = getZipSerialM
1045 fromStream = ZipSerialM
1070 fromStream = ZipSerialM
1071+ cons = consSerial
1072+ {-# INLINE consM #-}
1073+ {-# SPECIALIZE consM :: IO a -> ZipSerialM IO a -> ZipSerialM IO a #-}
1074+ consM = consMSerial
1046
1075
1047instance Monad m => Applicative (ZipSerialM m) where
1076instance Monad m => Applicative (ZipSerialM m) where
1048 pure = ZipSerialM . S.repeat
1077 pure = ZipSerialM . S.repeat
@@ -1075,6 +1104,10 @@ newtype ZipAsyncM m a = ZipAsyncM {getZipAsyncM :: S.Stream m a}
1075instance IsStream ZipAsyncM where
1104instance IsStream ZipAsyncM where
1076 toStream = getZipAsyncM
1105 toStream = getZipAsyncM
1077 fromStream = ZipAsyncM
1106 fromStream = ZipAsyncM
1107+ cons = consSerial
1108+ {-# INLINE consM #-}
1109+ {-# SPECIALIZE consM :: IO a -> ZipAsyncM IO a -> ZipAsyncM IO a #-}
1110+ consM = consMSerial
1078
1111
1079instance MonadAsync m => Applicative (ZipAsyncM m) where
1112instance MonadAsync m => Applicative (ZipAsyncM m) where
1080 pure = ZipAsyncM . S.repeat
1113 pure = ZipAsyncM . S.repeat