on a79caed help sign in
105 lines 3.0 kB history raw
1{-# OPTIONS_GHC -Wno-orphans #-}
3-- |
4-- Module : Streamly.Internal.Data.Stream.Common
5-- Copyright : (c) 2017 Composewell Technologies
6-- License : BSD-3-Clause
7-- Maintainer : streamly@composewell.com
8-- Stability : experimental
9-- Portability : GHC
10--
11-- Low level functions using StreamK as the intermediate stream type. These
12-- functions are used in other stream modules to implement their instances.
13--
14module Streamly.Internal.Data.Stream.Common
15 (
16 -- * Conversion operations
17 fromList
18 , toList
20 -- * Fold operations
21 , foldr
22 , foldl'
23 , fold
25 -- * Zip style operations
26 , eqBy
27 , cmpBy
28 )
29where
31#include "inline.hs"
33import Streamly.Data.Fold (Fold)
35import qualified Streamly.Data.StreamK as K
36import qualified Streamly.Internal.Data.Stream as D
38import Prelude hiding (Foldable(..), repeat)
40------------------------------------------------------------------------------
41-- Conversions
42------------------------------------------------------------------------------
44-- |
45-- @
46-- fromList = 'Prelude.foldr' 'K.cons' 'K.nil'
47-- @
48--
49-- Construct a stream from a list of pure values. This is more efficient than
50-- 'K.fromFoldable' for serial streams.
51--
52{-# INLINE_EARLY fromList #-}
53fromList :: Monad m => [a] -> K.StreamK m a
54fromList = D.toStreamK . D.fromList
55{-# RULES "fromList fallback to StreamK" [1]
56 forall a. D.toStreamK (D.fromList a) = K.fromFoldable a #-}
58-- | Convert a stream into a list in the underlying monad.
59--
60{-# INLINE toList #-}
61toList :: Monad m => K.StreamK m a -> m [a]
62toList m = D.toList $ D.fromStreamK m
64------------------------------------------------------------------------------
65-- Folds
66------------------------------------------------------------------------------
68{-# INLINE foldrM #-}
69foldrM :: Monad m => (a -> m b -> m b) -> m b -> K.StreamK m a -> m b
70foldrM step acc m = D.foldrM step acc $ D.fromStreamK m
72{-# INLINE foldr #-}
73foldr :: Monad m => (a -> b -> b) -> b -> K.StreamK m a -> m b
74foldr f z = foldrM (\a b -> f a <$> b) (return z)
76-- | Strict left associative fold.
77--
78{-# INLINE foldl' #-}
79foldl' ::
80 Monad m => (b -> a -> b) -> b -> K.StreamK m a -> m b
81foldl' step begin m = D.foldl' step begin $ D.fromStreamK m
84{-# INLINE fold #-}
85fold :: Monad m => Fold m a b -> K.StreamK m a -> m b
86fold fld m = D.fold fld $ D.fromStreamK m
88------------------------------------------------------------------------------
89-- Comparison
90------------------------------------------------------------------------------
92-- | Compare two streams for equality
93--
94{-# INLINE eqBy #-}
95eqBy :: Monad m =>
96 (a -> b -> Bool) -> K.StreamK m a -> K.StreamK m b -> m Bool
97eqBy f m1 m2 = D.eqBy f (D.fromStreamK m1) (D.fromStreamK m2)
99-- | Compare two streams
100--
101{-# INLINE cmpBy #-}
102cmpBy
103 :: Monad m
104 => (a -> b -> Ordering) -> K.StreamK m a -> K.StreamK m b -> m Ordering
105cmpBy f m1 m2 = D.cmpBy f (D.fromStreamK m1) (D.fromStreamK m2)