1{-# OPTIONS_GHC -Wno-orphans #-}
6-- Module : Streamly.Internal.Data.Stream.Prelude
7-- Copyright : (c) 2017 Composewell Technologies
10-- Maintainer : streamly@composewell.com
11-- Stability : experimental
15module Streamly.Internal.Data.Stream.Prelude
17 -- * Stream Conversion
24 -- * Conversion operations
38 -- Lazy left folds are useful only for reversing the stream
49 -- * Zip style operations
53 -- * Foldable instance
63 , concatMapFoldableWith
64 , concatForFoldableWith
68import Control.Monad.Trans.Class (MonadTrans(..))
69import Prelude hiding (foldr, minimum, maximum)
70import qualified Prelude
72import Streamly.Internal.Data.Fold.Type (Fold (..))
74#ifdef USE_STREAMK_ONLY
75import qualified Streamly.Internal.Data.Stream.StreamK as S
77import qualified Streamly.Internal.Data.Stream.StreamD as S
80import Streamly.Internal.Data.Stream.StreamK (IsStream(..))
81import qualified Streamly.Internal.Data.Stream.StreamK as K
82import qualified Streamly.Internal.Data.Stream.StreamD as D
84------------------------------------------------------------------------------
85-- Conversion to and from direct style stream
86------------------------------------------------------------------------------
88-- These definitions are dependent on what is imported as S
89{-# INLINE fromStreamS #-}
90fromStreamS :: (IsStream t, Monad m) => S.Stream m a -> t m a
91fromStreamS = fromStream . S.toStreamK
93{-# INLINE toStreamS #-}
94toStreamS :: (IsStream t, Monad m) => t m a -> S.Stream m a
95toStreamS = S.fromStreamK . toStream
97------------------------------------------------------------------------------
99------------------------------------------------------------------------------
101{-# INLINE_EARLY drain #-}
102drain :: (IsStream t, Monad m) => t m a -> m ()
103drain m = D.drain $ D.fromStreamK (toStream m)
104{-# RULES "drain fallback to CPS" [1]
105 forall a. D.drain (D.fromStreamK a) = K.drain a #-}
107------------------------------------------------------------------------------
109------------------------------------------------------------------------------
113-- fromList = 'Prelude.foldr' 'K.cons' 'K.nil'
116-- Construct a stream from a list of pure values. This is more efficient than
117-- 'K.fromFoldable' for serial streams.
120{-# INLINE_EARLY fromList #-}
121fromList :: (Monad m, IsStream t) => [a] -> t m a
122fromList = fromStreamS . S.fromList
123{-# RULES "fromList fallback to StreamK" [1]
124 forall a. S.toStreamK (S.fromList a) = K.fromFoldable a #-}
126-- | Convert a stream into a list in the underlying monad.
130toList :: (Monad m, IsStream t) => t m a -> m [a]
131toList m = S.toList $ toStreamS m
133------------------------------------------------------------------------------
135------------------------------------------------------------------------------
138foldrM :: (Monad m, IsStream t) => (a -> m b -> m b) -> m b -> t m a -> m b
139foldrM step acc m = S.foldrM step acc $ toStreamS m
141{-# INLINE foldrMx #-}
142foldrMx :: (Monad m, IsStream t)
143 => (a -> m x -> m x) -> m x -> (m x -> m b) -> t m a -> m b
144foldrMx step final project m = D.foldrMx step final project $ D.toStreamD m
147foldr :: (Monad m, IsStream t) => (a -> b -> b) -> b -> t m a -> m b
148foldr f z = foldrM (\a b -> f a <$> b) (return z)
150-- | Like 'foldlx'', but with a monadic step function.
153{-# INLINE foldlMx' #-}
154foldlMx' :: (IsStream t, Monad m)
155 => (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> m b
156foldlMx' step begin done m = S.foldlMx' step begin done $ toStreamS m
158-- | Strict left fold with an extraction function. Like the standard strict
159-- left fold, but applies a user supplied extraction function (the third
160-- argument) to the folded value at the end. This is designed to work with the
161-- @foldl@ library. The suffix @x@ is a mnemonic for extraction.
164{-# INLINE foldlx' #-}
165foldlx' :: (IsStream t, Monad m)
166 => (x -> a -> x) -> x -> (x -> b) -> t m a -> m b
167foldlx' step begin done m = S.foldlx' step begin done $ toStreamS m
169-- | Strict left associative fold.
173foldl' :: (Monad m, IsStream t) => (b -> a -> b) -> b -> t m a -> m b
174foldl' step begin m = S.foldl' step begin $ toStreamS m
177foldlS :: IsStream t => (t m b -> a -> t m b) -> t m b -> t m a -> t m b
180-- | Lazy left fold to a transformer monad.
182-- For example, to reverse a stream:
184-- > S.toList $ S.foldlT (flip S.cons) S.nil $ (S.fromList [1..5] :: SerialT IO Int)
187foldlT :: (Monad m, IsStream t, Monad (s m), MonadTrans s)
188 => (s m b -> a -> s m b) -> s m b -> t m a -> s m b
189foldlT f z s = S.foldlT f z (toStreamS s)
192fold :: (Monad m, IsStream t) => Fold m a b -> t m a -> m b
193fold fld m = S.fold fld $ toStreamS m
195------------------------------------------------------------------------------
197------------------------------------------------------------------------------
199-- postscanlM' followed by mapM
200{-# INLINE postscanlMx' #-}
201postscanlMx' :: (IsStream t, Monad m)
202 => (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> t m b
203postscanlMx' step begin done m =
204 D.fromStreamD $ D.postscanlMx' step begin done $ D.toStreamD m
206-- postscanl' followed by map
207{-# INLINE postscanlx' #-}
208postscanlx' :: (IsStream t, Monad m)
209 => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
210postscanlx' step begin done m =
211 D.fromStreamD $ D.postscanlx' step begin done $ D.toStreamD m
213-- scanlM' followed by mapM
215{-# INLINE scanlMx' #-}
216scanlMx' :: (IsStream t, Monad m)
217 => (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> t m b
218scanlMx' step begin done m =
219 D.fromStreamD $ D.scanlMx' step begin done $ D.toStreamD m
221{-# INLINE_NORMAL postscanOnce #-}
222postscanOnce :: (IsStream t, Monad m)
223 => Fold m a b -> t m a -> t m b
225 D.fromStreamD $ D.postscanOnce fld $ D.toStreamD m
227{-# INLINE scanOnce #-}
228scanOnce :: (IsStream t, Monad m)
229 => Fold m a b -> t m a -> t m b
230scanOnce fld m = D.fromStreamD $ D.scanOnce fld $ D.toStreamD m
232-- scanl followed by map
234-- | Strict left scan with an extraction function. Like 'scanl'', but applies a
235-- user supplied extraction function (the third argument) at each step. This is
236-- designed to work with the @foldl@ library. The suffix @x@ is a mnemonic for
240{-# INLINE scanlx' #-}
241scanlx' :: (IsStream t, Monad m)
242 => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
243scanlx' step begin done m =
244 fromStreamS $ S.scanlx' step begin done $ toStreamS m
246------------------------------------------------------------------------------
248------------------------------------------------------------------------------
250-- | Compare two streams for equality
254eqBy :: (IsStream t, Monad m) => (a -> b -> Bool) -> t m a -> t m b -> m Bool
255eqBy f m1 m2 = D.eqBy f (D.toStreamD m1) (D.toStreamD m2)
257-- | Compare two streams
262 :: (IsStream t, Monad m)
263 => (a -> b -> Ordering) -> t m a -> t m b -> m Ordering
264cmpBy f m1 m2 = D.cmpBy f (D.toStreamD m1) (D.toStreamD m2)
266{-# INLINE minimum #-}
267minimum :: (IsStream t, Monad m, Ord a) => t m a -> m (Maybe a)
268minimum m = S.minimum (toStreamS m)
270{-# INLINE maximum #-}
271maximum :: (IsStream t, Monad m, Ord a) => t m a -> m (Maybe a)
272maximum m = S.maximum (toStreamS m)
274------------------------------------------------------------------------------
276------------------------------------------------------------------------------
279-- XXX do we have facilities in Foldable to fold any Foldable in this manner?
281-- | Perform a pair wise bottom up hierarchical fold of elements in the
282-- container using the given function as the merge function.
284-- This will perform a balanced merge sort if the merge function is
288{-# INLINABLE foldbWith #-}
289foldbWith :: IsStream t
290 => (t m a -> t m a -> t m a) -> SerialT Identity (t m a) -> t m a
291foldbWith f = K.foldb f K.nil
295-- | A variant of 'Data.Foldable.fold' that allows you to fold a 'Foldable'
296-- container of streams using the specified stream sum operation.
298-- @concatFoldableWith 'async' $ map return [1..3]@
303-- concatFoldableWith f = Prelude.foldr f S.nil
304-- concatFoldableWith f = S.concatMapFoldableWith f id
307-- /Since: 0.8.0 (Renamed foldWith to concatFoldableWith)/
309-- /Since: 0.1.0 ("Streamly")/
310{-# INLINABLE concatFoldableWith #-}
311concatFoldableWith :: (IsStream t, Foldable f)
312 => (t m a -> t m a -> t m a) -> f (t m a) -> t m a
313concatFoldableWith f = Prelude.foldr f K.nil
315-- | A variant of 'foldMap' that allows you to map a monadic streaming action
316-- on a 'Foldable' container and then fold it using the specified stream merge
319-- @concatMapFoldableWith 'async' return [1..3]@
324-- concatMapFoldableWith f g = Prelude.foldr (f . g) S.nil
325-- concatMapFoldableWith f g xs = S.concatMapWith f g (S.fromFoldable xs)
328-- /Since: 0.8.0 (Renamed foldMapWith to concatMapFoldableWith)/
330-- /Since: 0.1.0 ("Streamly")/
331{-# INLINABLE concatMapFoldableWith #-}
332concatMapFoldableWith :: (IsStream t, Foldable f)
333 => (t m b -> t m b -> t m b) -> (a -> t m b) -> f a -> t m b
334concatMapFoldableWith f g = Prelude.foldr (f . g) K.nil
336-- | Like 'concatMapFoldableWith' but with the last two arguments reversed i.e. the
337-- monadic streaming function is the last argument.
342-- concatForFoldableWith f xs g = Prelude.foldr (f . g) S.nil xs
343-- concatForFoldableWith = flip S.concatMapFoldableWith
346-- /Since: 0.8.0 (Renamed forEachWith to concatForFoldableWith)/
348-- /Since: 0.1.0 ("Streamly")/
349{-# INLINABLE concatForFoldableWith #-}
350concatForFoldableWith :: (IsStream t, Foldable f)
351 => (t m b -> t m b -> t m b) -> f a -> (a -> t m b) -> t m b
352concatForFoldableWith f xs g = Prelude.foldr (f . g) K.nil xs
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list