1{-# OPTIONS_GHC -Wno-orphans #-}
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
11-- Low level functions using StreamK as the intermediate stream type. These
12-- functions are used in other stream modules to implement their instances.
14module Streamly.Internal.Data.Stream.Common
16 -- * Conversion operations
25 -- * Zip style operations
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------------------------------------------------------------------------------
42------------------------------------------------------------------------------
46-- fromList = 'Prelude.foldr' 'K.cons' 'K.nil'
49-- Construct a stream from a list of pure values. This is more efficient than
50-- 'K.fromFoldable' for serial streams.
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.
61toList :: Monad m => K.StreamK m a -> m [a]
62toList m = D.toList $ D.fromStreamK m
64------------------------------------------------------------------------------
66------------------------------------------------------------------------------
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
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.
80 Monad m => (b -> a -> b) -> b -> K.StreamK m a -> m b
81foldl' step begin m = D.foldl' step begin $ D.fromStreamK m
85fold :: Monad m => Fold m a b -> K.StreamK m a -> m b
86fold fld m = D.fold fld $ D.fromStreamK m
88------------------------------------------------------------------------------
90------------------------------------------------------------------------------
92-- | Compare two streams for equality
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
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)
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