on master help sign in
Add conversion from a general stream to pure stream
commitfebb5a1e5c3e8a7edc1ef68809f94b89c0bb9a17 authorHarendra Kumar <harendra.kumar@gmail.com> dateSat 9 Nov 2019, 18:20 (7 years ago) parente5602f1
1 file changed+43−15
src/Streamly/Internal/Prelude.hs+43−15
src/Streamly/Internal/Prelude.hs+43−15
@@ -78,9 +78,6 @@ module Streamly.Internal.Prelude
7878 , foldl1'
7979 , foldlM'
8080
81− -- ** Composable Left Folds
82− , fold
83−
8481 -- ** Full Folds
8582
8683 -- -- ** To Summary (Full Folds)
@@ -97,14 +94,6 @@ module Streamly.Internal.Prelude
9794 , minimumBy
9895 , minimum
9996 , the
100− , toListRev -- experimental
101− , toStream
102− , toStreamRev
103−
104− -- ** Lazy Folds
105−
106− -- -- ** To Containers (Full Folds)
107− , toList
10897
10998 -- ** Partial Folds
11099
@@ -130,6 +119,18 @@ module Streamly.Internal.Prelude
130119 , and
131120 , or
132121
122+ -- ** To Containers
123+ , toList
124+ , toListRev
125+ , toPure
126+ , toPureRev
127+
128+ -- ** Composable Left Folds
129+ , fold
130+
131+ , toStream -- XXX rename to write?
132+ , toStreamRev -- XXX rename to writeRev?
133+
133134 -- * Transformation
134135 , transform
135136
@@ -397,7 +398,7 @@ import Control.Monad (void)
397398import Control.Monad.Catch (MonadCatch)
398399import Control.Monad.IO.Class (MonadIO(..))
399400import Control.Monad.Trans (MonadTrans(..))
400−import Data.Functor.Identity (Identity)
401+import Data.Functor.Identity (Identity (..))
401402import Data.Heap (Entry(..))
402403import Data.Maybe (isJust, fromJust, isNothing)
403404import Foreign.Storable (Storable)
@@ -1380,7 +1381,7 @@ toList = P.toList
13801381-- /Warning!/ working on large lists accumulated as buffers in memory could be
13811382-- very inefficient, consider using "Streamly.Array" instead.
13821383--
1383−-- @since 0.7.0
1384+-- /Internal/
13841385{-# INLINE toListRev #-}
13851386toListRev :: Monad m => SerialT m a -> m [a]
13861387toListRev = D.toListRev . toStreamD
@@ -1404,12 +1405,15 @@ toHandle h m = go m
14041405 yieldk a r = liftIO (IO.hPutStrLn h a) >> go r
14051406 in K.foldStream defState yieldk single stop m1
14061407
1408+-- XXX rename these to write/writeRev to make the naming consistent with folds
1409+-- in other modules.
1410+--
14071411-- | A fold that buffers its input to a pure stream.
14081412--
14091413-- /Warning!/ working on large streams accumulated as buffers in memory could
14101414-- be very inefficient, consider using "Streamly.Array" instead.
14111415--
1412−-- @since 0.7.0
1416+-- /Internal/
14131417{-# INLINE toStream #-}
14141418toStream :: Monad m => Fold m a (SerialT Identity a)
14151419toStream = Fold (\f x -> return $ f . (x `K.cons`))
@@ -1425,13 +1429,37 @@ toStream = Fold (\f x -> return $ f . (x `K.cons`))
14251429-- /Warning!/ working on large streams accumulated as buffers in memory could
14261430-- be very inefficient, consider using "Streamly.Array" instead.
14271431--
1428−-- @since 0.7.0
1432+-- /Internal/
14291433
14301434-- xn : ... : x2 : x1 : []
14311435{-# INLINABLE toStreamRev #-}
14321436toStreamRev :: Monad m => Fold m a (SerialT Identity a)
14331437toStreamRev = Fold (\xs x -> return $ x `K.cons` xs) (return K.nil) return
14341438
1439+-- | Convert a stream to a pure stream.
1440+--
1441+-- @
1442+-- toPure = foldr cons nil
1443+-- @
1444+--
1445+-- /Internal/
1446+--
1447+{-# INLINE toPure #-}
1448+toPure :: Monad m => SerialT m a -> m (SerialT Identity a)
1449+toPure = foldr K.cons K.nil
1450+
1451+-- | Convert a stream to a pure stream in reverse order.
1452+--
1453+-- @
1454+-- toPureRev = foldl' (flip cons) nil
1455+-- @
1456+--
1457+-- /Internal/
1458+--
1459+{-# INLINE toPureRev #-}
1460+toPureRev :: Monad m => SerialT m a -> m (SerialT Identity a)
1461+toPureRev = foldl' (flip K.cons) K.nil
1462+
14351463------------------------------------------------------------------------------
14361464-- General Transformation
14371465------------------------------------------------------------------------------