on d0ed396 help sign in
113 lines 3.3 kB history raw
1{-# LANGUAGE UndecidableInstances #-}
2{-# OPTIONS_GHC -Wno-orphans #-}
4-- |
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
10-- Portability : GHC
11--
12-- Low level functions using StreamK as the intermediate stream type. These
13-- functions are used in other stream modules to implement their instances.
14--
15module Streamly.Internal.Data.Stream.Common
16 (
17 -- * Conversion operations
18 fromList
19 , toList
21 -- * Fold operations
22 , foldr
23 , foldl'
24 , fold
26 -- * Zip style operations
27 , eqBy
28 , cmpBy
29 )
30where
32#include "inline.hs"
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
39#else
40import qualified Streamly.Internal.Data.Stream.StreamD.Type as S
41#endif
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------------------------------------------------------------------------------
49-- Conversions
50------------------------------------------------------------------------------
52-- |
53-- @
54-- fromList = 'Prelude.foldr' 'K.cons' 'K.nil'
55-- @
56--
57-- Construct a stream from a list of pure values. This is more efficient than
58-- 'K.fromFoldable' for serial streams.
59--
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.
67--
68{-# INLINE toList #-}
69toList :: Monad m => K.Stream m a -> m [a]
70toList m = S.toList $ S.fromStreamK m
72------------------------------------------------------------------------------
73-- Folds
74------------------------------------------------------------------------------
76{-# INLINE foldrM #-}
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
80{-# INLINE foldr #-}
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.
85--
86{-# INLINE foldl' #-}
87foldl' ::
88 Monad m => (b -> a -> b) -> b -> K.Stream m a -> m b
89foldl' step begin m = S.foldl' step begin $ S.fromStreamK m
92{-# INLINE fold #-}
93fold :: Monad m => Fold m a b -> K.Stream m a -> m b
94fold fld m = S.fold fld $ S.fromStreamK m
96------------------------------------------------------------------------------
97-- Comparison
98------------------------------------------------------------------------------
100-- | Compare two streams for equality
101--
102{-# INLINE eqBy #-}
103eqBy :: Monad m =>
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
108--
109{-# INLINE cmpBy #-}
110cmpBy
111 :: Monad m
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)