3#if __GLASGOW_HASKELL__ >= 800
4{-# OPTIONS_GHC -Wno-orphans #-}
10-- Module : Streamly.Streams.Prelude
11-- Copyright : (c) 2017 Harendra Kumar
14-- Maintainer : harendra.kumar@gmail.com
15-- Stability : experimental
19module Streamly.Streams.Prelude
21 -- * Stream Conversion
25 -- * Conversion operations
32 -- * Zip style operations
43#ifdef USE_STREAMK_ONLY
44import qualified Streamly.Streams.StreamK as S
46import qualified Streamly.Streams.StreamD as S
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------------------------------------------------------------------------------
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
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.
85toList :: (Monad m, IsStream t) => t m a -> m [a]
86toList m = S.toList $ toStreamS m
88------------------------------------------------------------------------------
90------------------------------------------------------------------------------
92-- | Strict left associative fold.
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------------------------------------------------------------------------------
101------------------------------------------------------------------------------
103-- | Compare two streams for equality
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
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------------------------------------------------------------------------------
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.
126-- @foldWith 'async' $ map return [1..3]@
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
138-- @foldMapWith 'async' return [1..3]@
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.
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
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