on 770887d help sign in
106 lines 3.0 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 (..))
36import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
37import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
39import Prelude hiding (foldr, repeat)
41------------------------------------------------------------------------------
42-- Conversions
43------------------------------------------------------------------------------
45-- |
46-- @
47-- fromList = 'Prelude.foldr' 'K.cons' 'K.nil'
48-- @
49--
50-- Construct a stream from a list of pure values. This is more efficient than
51-- 'K.fromFoldable' for serial streams.
52--
53{-# INLINE_EARLY fromList #-}
54fromList :: Monad m => [a] -> K.Stream m a
55fromList = D.toStreamK . D.fromList
56{-# RULES "fromList fallback to StreamK" [1]
57 forall a. D.toStreamK (D.fromList a) = K.fromFoldable a #-}
59-- | Convert a stream into a list in the underlying monad.
60--
61{-# INLINE toList #-}
62toList :: Monad m => K.Stream m a -> m [a]
63toList m = D.toList $ D.fromStreamK m
65------------------------------------------------------------------------------
66-- Folds
67------------------------------------------------------------------------------
69{-# INLINE foldrM #-}
70foldrM :: Monad m => (a -> m b -> m b) -> m b -> K.Stream m a -> m b
71foldrM step acc m = D.foldrM step acc $ D.fromStreamK m
73{-# INLINE foldr #-}
74foldr :: Monad m => (a -> b -> b) -> b -> K.Stream m a -> m b
75foldr f z = foldrM (\a b -> f a <$> b) (return z)
77-- | Strict left associative fold.
78--
79{-# INLINE foldl' #-}
80foldl' ::
81 Monad m => (b -> a -> b) -> b -> K.Stream m a -> m b
82foldl' step begin m = D.foldl' step begin $ D.fromStreamK m
85{-# INLINE fold #-}
86fold :: Monad m => Fold m a b -> K.Stream m a -> m b
87fold fld m = D.fold fld $ D.fromStreamK m
89------------------------------------------------------------------------------
90-- Comparison
91------------------------------------------------------------------------------
93-- | Compare two streams for equality
94--
95{-# INLINE eqBy #-}
96eqBy :: Monad m =>
97 (a -> b -> Bool) -> K.Stream m a -> K.Stream m b -> m Bool
98eqBy f m1 m2 = D.eqBy f (D.fromStreamK m1) (D.fromStreamK m2)
100-- | Compare two streams
101--
102{-# INLINE cmpBy #-}
103cmpBy
104 :: Monad m
105 => (a -> b -> Ordering) -> K.Stream m a -> K.Stream m b -> m Ordering
106cmpBy f m1 m2 = D.cmpBy f (D.fromStreamK m1) (D.fromStreamK m2)