@@ -140,8 +140,6 @@ import qualified Streamly.Internal.Data.Stream.ZipAsync as ZipAsync
140import Prelude hiding (foldr, repeat)
140import Prelude hiding (foldr, repeat)
142#include "inline.hs"
142#include "inline.hs"
143−#define USE_IS_STREAM
144−#include "PreludeCommon.hs"
146------------------------------------------------------------------------------
144------------------------------------------------------------------------------
147-- Types that can behave as a Stream
145-- Types that can behave as a Stream
@@ -231,8 +229,8 @@ adapt :: (IsStream t1, IsStream t2) => t1 m a -> t2 m a
231adapt = fromStream . toStream
229adapt = fromStream . toStream
233{-# INLINE fromStreamD #-}
231{-# INLINE fromStreamD #-}
234−fromStreamD :: (IS_STREAM Monad m) => D.Stream m a -> STREAM m a
232+fromStreamD :: (IsStream t, Monad m) => D.Stream m a -> t m a
235−fromStreamD = FROM_STREAM . D.toStreamK
233+fromStreamD = fromStream . D.toStreamK
237-- | Adapt a polymorphic consM operation to a StreamK cons operation
235-- | Adapt a polymorphic consM operation to a StreamK cons operation
238{-# INLINE toConsK #-}
236{-# INLINE toConsK #-}
@@ -240,6 +238,80 @@ toConsK :: IsStream t =>
240 (m a -> t m a -> t m a) -> m a -> K.Stream m a -> K.Stream m a
238 (m a -> t m a -> t m a) -> m a -> K.Stream m a -> K.Stream m a
241toConsK cns x xs = toStream $ x `cns` fromStream xs
239toConsK cns x xs = toStream $ x `cns` fromStream xs
241+------------------------------------------------------------------------------
242+-- Conversion to and from direct style stream
243+------------------------------------------------------------------------------
245+-- These definitions are dependent on what is imported as S
246+{-# INLINE fromStreamS #-}
247+fromStreamS :: (IsStream t, Monad m) => S.Stream m a -> t m a
248+fromStreamS = fromStream . S.toStreamK
250+{-# INLINE toStreamS #-}
251+toStreamS :: (IsStream t, Monad m) => t m a -> S.Stream m a
252+toStreamS = S.fromStreamK . toStream
254+{-# INLINE toStreamD #-}
255+toStreamD :: (IsStream t, Monad m) => t m a -> D.Stream m a
256+toStreamD = D.fromStreamK . toStream
258+------------------------------------------------------------------------------
260+------------------------------------------------------------------------------
262+{-# INLINE_EARLY drain #-}
263+drain :: (IsStream t, Monad m) => t m a -> m ()
264+drain m = D.drain $ D.fromStreamK (toStream m)
265+{-# RULES "drain fallback to CPS" [1]
266+ forall a. D.drain (D.fromStreamK a) = K.drain a #-}
268+------------------------------------------------------------------------------
270+------------------------------------------------------------------------------
272+-- | Compare two streams for equality
276+eqBy :: (IsStream t, Monad m) =>
277+ (a -> b -> Bool) -> t m a -> t m b -> m Bool
278+eqBy f m1 m2 = D.eqBy f (toStreamD m1) (toStreamD m2)
280+-- | Compare two streams
285+ :: (IsStream t, Monad m)
286+ => (a -> b -> Ordering) -> t m a -> t m b -> m Ordering
287+cmpBy f m1 m2 = D.cmpBy f (toStreamD m1) (toStreamD m2)
289+------------------------------------------------------------------------------
291+------------------------------------------------------------------------------
295+-- fromList = 'Prelude.foldr' 'K.cons' 'K.nil'
298+-- Construct a stream from a list of pure values. This is more efficient than
299+-- 'K.fromFoldable' for serial streams.
302+{-# INLINE_EARLY fromList #-}
303+fromList :: (Monad m, IsStream t) => [a] -> t m a
304+fromList = fromStreamS . S.fromList
305+{-# RULES "fromList fallback to StreamK" [1]
306+ forall a. S.toStreamK (S.fromList a) = K.fromFoldable a #-}
308+-- | Convert a stream into a list in the underlying monad.
311+{-# INLINE toList #-}
312+toList :: (IsStream t, Monad m) => t m a -> m [a]
313+toList m = S.toList $ toStreamS m
243------------------------------------------------------------------------------
315------------------------------------------------------------------------------
244-- Building a stream
316-- Building a stream
245------------------------------------------------------------------------------
317------------------------------------------------------------------------------
@@ -288,6 +360,56 @@ mkStreamStream
288 -> K.Stream m a
360 -> K.Stream m a
289mkStreamStream = K.MkStream
361mkStreamStream = K.MkStream
363+------------------------------------------------------------------------------
365+------------------------------------------------------------------------------
367+{-# INLINE foldrM #-}
368+foldrM :: (IsStream t, Monad m) => (a -> m b -> m b) -> m b -> t m a -> m b
369+foldrM step acc m = S.foldrM step acc $ toStreamS m
371+{-# INLINE foldrMx #-}
372+foldrMx :: (IsStream t, Monad m)
373+ => (a -> m x -> m x) -> m x -> (m x -> m b) -> t m a -> m b
374+foldrMx step final project m = D.foldrMx step final project $ toStreamD m
377+foldr :: (IsStream t, Monad m) => (a -> b -> b) -> b -> t m a -> m b
378+foldr f z = foldrM (\a b -> f a <$> b) (return z)
380+-- | Like 'foldlx'', but with a monadic step function.
383+{-# INLINE foldlMx' #-}
385+ (IsStream t, Monad m)
386+ => (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> m b
387+foldlMx' step begin done m = S.foldlMx' step begin done $ toStreamS m
389+-- | Strict left fold with an extraction function. Like the standard strict
390+-- left fold, but applies a user supplied extraction function (the third
391+-- argument) to the folded value at the end. This is designed to work with the
392+-- @foldl@ library. The suffix @x@ is a mnemonic for extraction.
395+{-# INLINE foldlx' #-}
397+ (IsStream t, Monad m) => (x -> a -> x) -> x -> (x -> b) -> t m a -> m b
398+foldlx' step begin done m = S.foldlx' step begin done $ toStreamS m
400+-- | Strict left associative fold.
403+{-# INLINE foldl' #-}
405+ (IsStream t, Monad m) => (b -> a -> b) -> b -> t m a -> m b
406+foldl' step begin m = S.foldl' step begin $ toStreamS m
410+fold :: (IsStream t, Monad m) => Fold m a b -> t m a -> m b
411+fold fld m = S.fold fld $ toStreamS m
291------------------------------------------------------------------------------
413------------------------------------------------------------------------------
292-- Folding a stream
414-- Folding a stream
293------------------------------------------------------------------------------
415------------------------------------------------------------------------------