on 0839d4f help sign in
153 lines 4.5 kB history raw
1{-# LANGUAGE CPP #-}
3#if __GLASGOW_HASKELL__ >= 800
4{-# OPTIONS_GHC -Wno-orphans #-}
5#endif
7#include "inline.hs"
9-- |
10-- Module : Streamly.Streams.Prelude
11-- Copyright : (c) 2017 Harendra Kumar
12--
13-- License : BSD3
14-- Maintainer : harendra.kumar@gmail.com
15-- Stability : experimental
16-- Portability : GHC
17--
18--
19module Streamly.Streams.Prelude
20 (
21 -- * Stream Conversion
22 fromStreamS
23 , toStreamS
25 -- * Conversion operations
26 , fromList
27 , toList
29 -- * Fold operations
30 , foldl'
32 -- * Zip style operations
33 , eqBy
34 , cmpBy
36 -- * Fold Utilities
37 , foldWith
38 , foldMapWith
39 , forEachWith
40 )
41where
43#ifdef USE_STREAMK_ONLY
44import qualified Streamly.Streams.StreamK as S
45#else
46import qualified Streamly.Streams.StreamD as S
47#endif
49import Streamly.Streams.StreamK (IsStream(..))
50import qualified Streamly.Streams.StreamK as K
51import qualified Streamly.Streams.StreamD as D
53------------------------------------------------------------------------------
54-- Conversion to and from direct style stream
55------------------------------------------------------------------------------
57-- These definitions are dependent on what is imported as S
58{-# INLINE fromStreamS #-}
59fromStreamS :: (IsStream t, Monad m) => S.Stream m a -> t m a
60fromStreamS = fromStream . S.toStreamK
62{-# INLINE toStreamS #-}
63toStreamS :: (IsStream t, Monad m) => t m a -> S.Stream m a
64toStreamS = S.fromStreamK . toStream
66------------------------------------------------------------------------------
67-- Conversions
68------------------------------------------------------------------------------
70-- | Construct a stream from a list containing pure values. More efficient list
71-- specific implementation of 'K.fromFoldable' as it works well with fusion
72-- optimization.
73--
74-- @since 0.4.0
75{-# INLINE_EARLY fromList #-}
76fromList :: (Monad m, IsStream t) => [a] -> t m a
77fromList = fromStreamS . S.fromList
78{-# RULES "fromList fallback to StreamK" [1]
79 forall a. S.toStreamK (S.fromList a) = K.fromFoldable a #-}
81-- | Convert a stream into a list in the underlying monad.
82--
83-- @since 0.1.0
84{-# INLINE toList #-}
85toList :: (Monad m, IsStream t) => t m a -> m [a]
86toList m = S.toList $ toStreamS m
88------------------------------------------------------------------------------
89-- Folds
90------------------------------------------------------------------------------
92-- | Strict left associative fold.
93--
94-- @since 0.2.0
95{-# INLINE foldl' #-}
96foldl' :: (Monad m, IsStream t) => (b -> a -> b) -> b -> t m a -> m b
97foldl' step begin m = S.foldl' step begin $ toStreamS m
99------------------------------------------------------------------------------
100-- Comparison
101------------------------------------------------------------------------------
103-- | Compare two streams for equality
104--
105-- @since 0.5.3
106{-# INLINE eqBy #-}
107eqBy :: (IsStream t, Monad m) => (a -> b -> Bool) -> t m a -> t m b -> m Bool
108eqBy f m1 m2 = D.eqBy f (D.toStreamD m1) (D.toStreamD m2)
110-- | Compare two streams
111--
112-- @since 0.5.3
113{-# INLINE cmpBy #-}
114cmpBy
115 :: (IsStream t, Monad m)
116 => (a -> b -> Ordering) -> t m a -> t m b -> m Ordering
117cmpBy f m1 m2 = D.cmpBy f (D.toStreamD m1) (D.toStreamD m2)
119------------------------------------------------------------------------------
120-- Fold Utilities
121------------------------------------------------------------------------------
123-- | A variant of 'Data.Foldable.fold' that allows you to fold a 'Foldable'
124-- container of streams using the specified stream sum operation.
125--
126-- @foldWith 'async' $ map return [1..3]@
127--
128-- @since 0.1.0
129{-# INLINABLE foldWith #-}
130foldWith :: (IsStream t, Foldable f)
131 => (t m a -> t m a -> t m a) -> f (t m a) -> t m a
132foldWith f = foldr f K.nil
134-- | A variant of 'foldMap' that allows you to map a monadic streaming action
135-- on a 'Foldable' container and then fold it using the specified stream sum
136-- operation.
137--
138-- @foldMapWith 'async' return [1..3]@
139--
140-- @since 0.1.0
141{-# INLINABLE foldMapWith #-}
142foldMapWith :: (IsStream t, Foldable f)
143 => (t m b -> t m b -> t m b) -> (a -> t m b) -> f a -> t m b
144foldMapWith f g = foldr (f . g) K.nil
146-- | Like 'foldMapWith' but with the last two arguments reversed i.e. the
147-- monadic streaming function is the last argument.
148--
149-- @since 0.1.0
150{-# INLINABLE forEachWith #-}
151forEachWith :: (IsStream t, Foldable f)
152 => (t m b -> t m b -> t m b) -> f a -> (a -> t m b) -> t m b
153forEachWith f xs g = foldr (f . g) K.nil xs