1{-# LANGUAGE UndecidableInstances #-}
2{-# OPTIONS_GHC -Wno-orphans #-}
5-- Module : Streamly.Internal.Data.Stream.Common
6-- Copyright : (c) 2017 Composewell Technologies
7-- License : BSD-3-Clause
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
12-- Low level functions using StreamK as the intermediate stream type. These
13-- functions are used in other stream modules to implement their instances.
15module Streamly.Internal.Data.Stream.Common
20 -- * Conversion operations
34 -- * Zip style operations
42import Streamly.Internal.Data.Fold.Type (Fold (..))
44#ifdef USE_STREAMK_ONLY
45import qualified Streamly.Internal.Data.Stream.StreamK as S
46import qualified Streamly.Internal.Data.Stream.StreamK.Type as S
48import qualified Streamly.Internal.Data.Stream.StreamD.Type as S
51import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
52import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
54import Prelude hiding (foldr, repeat)
56------------------------------------------------------------------------------
58------------------------------------------------------------------------------
60{-# INLINE_EARLY drain #-}
61drain :: Monad m => K.Stream m a -> m ()
62drain m = D.drain $ D.fromStreamK m
63{-# RULES "drain fallback to CPS" [1]
64 forall a. D.drain (D.fromStreamK a) = K.drain a #-}
66------------------------------------------------------------------------------
68------------------------------------------------------------------------------
72-- fromList = 'Prelude.foldr' 'K.cons' 'K.nil'
75-- Construct a stream from a list of pure values. This is more efficient than
76-- 'K.fromFoldable' for serial streams.
79{-# INLINE_EARLY fromList #-}
80fromList :: Monad m => [a] -> K.Stream m a
81fromList = S.toStreamK . S.fromList
82{-# RULES "fromList fallback to StreamK" [1]
83 forall a. S.toStreamK (S.fromList a) = K.fromFoldable a #-}
85-- | Convert a stream into a list in the underlying monad.
89toList :: Monad m => K.Stream m a -> m [a]
90toList m = S.toList $ S.fromStreamK m
92------------------------------------------------------------------------------
94------------------------------------------------------------------------------
97foldrM :: Monad m => (a -> m b -> m b) -> m b -> K.Stream m a -> m b
98foldrM step acc m = S.foldrM step acc $ S.fromStreamK m
100{-# INLINE foldrMx #-}
102 => (a -> m x -> m x) -> m x -> (m x -> m b) -> K.Stream m a -> m b
103foldrMx step final project m = D.foldrMx step final project $ D.fromStreamK m
106foldr :: Monad m => (a -> b -> b) -> b -> K.Stream m a -> m b
107foldr f z = foldrM (\a b -> f a <$> b) (return z)
109-- | Like 'foldlx'', but with a monadic step function.
112{-# INLINE foldlMx' #-}
115 => (x -> a -> m x) -> m x -> (x -> m b) -> K.Stream m a -> m b
116foldlMx' step begin done m = S.foldlMx' step begin done $ S.fromStreamK m
118-- | Strict left fold with an extraction function. Like the standard strict
119-- left fold, but applies a user supplied extraction function (the third
120-- argument) to the folded value at the end. This is designed to work with the
121-- @foldl@ library. The suffix @x@ is a mnemonic for extraction.
124{-# INLINE foldlx' #-}
126 Monad m => (x -> a -> x) -> x -> (x -> b) -> K.Stream m a -> m b
127foldlx' step begin done m = S.foldlx' step begin done $ S.fromStreamK m
129-- | Strict left associative fold.
134 Monad m => (b -> a -> b) -> b -> K.Stream m a -> m b
135foldl' step begin m = S.foldl' step begin $ S.fromStreamK m
139fold :: Monad m => Fold m a b -> K.Stream m a -> m b
140fold fld m = S.fold fld $ S.fromStreamK m
142------------------------------------------------------------------------------
144------------------------------------------------------------------------------
146-- | Compare two streams for equality
151 (a -> b -> Bool) -> K.Stream m a -> K.Stream m b -> m Bool
152eqBy f m1 m2 = D.eqBy f (D.fromStreamK m1) (D.fromStreamK m2)
154-- | Compare two streams
160 => (a -> b -> Ordering) -> K.Stream m a -> K.Stream m b -> m Ordering
161cmpBy f m1 m2 = D.cmpBy f (D.fromStreamK m1) (D.fromStreamK m2)
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