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
17 -- * Conversion operations
26 -- * Zip style operations
34import Streamly.Internal.Data.Fold.Type (Fold (..))
36#ifdef USE_STREAMK_ONLY
37import qualified Streamly.Internal.Data.Stream.StreamK as S
38import qualified Streamly.Internal.Data.Stream.StreamK.Type as S
40import qualified Streamly.Internal.Data.Stream.StreamD.Type as S
43import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
44import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
46import Prelude hiding (foldr, repeat)
48------------------------------------------------------------------------------
50------------------------------------------------------------------------------
54-- fromList = 'Prelude.foldr' 'K.cons' 'K.nil'
57-- Construct a stream from a list of pure values. This is more efficient than
58-- 'K.fromFoldable' for serial streams.
60{-# INLINE_EARLY fromList #-}
61fromList :: Monad m => [a] -> K.Stream m a
62fromList = S.toStreamK . S.fromList
63{-# RULES "fromList fallback to StreamK" [1]
64 forall a. S.toStreamK (S.fromList a) = K.fromFoldable a #-}
66-- | Convert a stream into a list in the underlying monad.
69toList :: Monad m => K.Stream m a -> m [a]
70toList m = S.toList $ S.fromStreamK m
72------------------------------------------------------------------------------
74------------------------------------------------------------------------------
77foldrM :: Monad m => (a -> m b -> m b) -> m b -> K.Stream m a -> m b
78foldrM step acc m = S.foldrM step acc $ S.fromStreamK m
81foldr :: Monad m => (a -> b -> b) -> b -> K.Stream m a -> m b
82foldr f z = foldrM (\a b -> f a <$> b) (return z)
84-- | Strict left associative fold.
88 Monad m => (b -> a -> b) -> b -> K.Stream m a -> m b
89foldl' step begin m = S.foldl' step begin $ S.fromStreamK m
93fold :: Monad m => Fold m a b -> K.Stream m a -> m b
94fold fld m = S.fold fld $ S.fromStreamK m
96------------------------------------------------------------------------------
98------------------------------------------------------------------------------
100-- | Compare two streams for equality
104 (a -> b -> Bool) -> K.Stream m a -> K.Stream m b -> m Bool
105eqBy f m1 m2 = D.eqBy f (D.fromStreamK m1) (D.fromStreamK m2)
107-- | Compare two streams
112 => (a -> b -> Ordering) -> K.Stream m a -> K.Stream m b -> m Ordering
113cmpBy 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