2-- Module : Streamly.Internal.Data.Stream.Generate
3-- Copyright : (c) 2017 Composewell Technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
9module Streamly.Internal.Data.Stream.Generate
18#if __GLASGOW_HASKELL__ < 808
19import Data.Semigroup (Semigroup(..))
21#if __GLASGOW_HASKELL__ < 808
22import Data.Semigroup (Semigroup(..))
24import Streamly.Internal.Data.Stream.Type (Stream)
25import Streamly.Internal.Data.Unfold (Unfold)
27import qualified Streamly.Internal.Data.Stream.Type as Stream
28import qualified Streamly.Internal.Data.Stream.StreamD.Type as StreamD
29import qualified Streamly.Internal.Data.Stream.StreamK.Type as StreamK
33-- >>> import qualified Streamly.Data.Fold as Fold
34-- >>> import qualified Streamly.Data.Unfold as Unfold
35-- >>> import qualified Streamly.Internal.Data.Stream as Stream
39-- | Construct a stream by adding a pure value at the head of an existing
40-- stream. Same as the following but more efficient:
44-- >> s = 1 `Stream.cons` 2 `Stream.cons` 3 `Stream.cons` Stream.nil
45-- >> Stream.fold Fold.toList s
48-- >>> cons x xs = return x `Stream.consM` xs
53cons :: a -> Stream m a -> Stream m a
54cons x = Stream.fromStreamK . StreamK.cons x . Stream.toStreamK
58-- | Constructs a stream by adding a monadic action at the head of an
59-- existing stream. For example:
61-- >> s = putChar 'h' `Stream.consM` putChar 'i' `Stream.consM` Stream.nil
62-- >> Stream.fold Fold.toList s
65-- >> consM x xs = Stream.fromEffect x `Stream.append` xs
70{-# SPECIALIZE consM :: IO a -> Stream IO a -> Stream IO a #-}
71consM :: Monad m => m a -> Stream m a -> Stream m a
72consM m = Stream.fromStreamK . StreamK.consM m . Stream.toStreamK
74------------------------------------------------------------------------------
76------------------------------------------------------------------------------
78-- | Convert an 'Unfold' into a stream by supplying it an input seed.
80-- >>> s = Stream.unfold (Unfold.replicateM 3) (putStrLn "hello")
81-- >>> Stream.fold Fold.drain s
88unfold :: Monad m => Unfold m a b -> a -> Stream m b
89unfold unf = Stream.fromStreamD . StreamD.unfold unf
91------------------------------------------------------------------------------
93------------------------------------------------------------------------------
97-- | Appends two streams sequentially, yielding all elements from the first
98-- stream, and then all elements from the second stream.
100-- >>> s1 = Stream.unfold Unfold.fromList [1,2]
101-- >>> s2 = Stream.unfold Unfold.fromList [3,4]
102-- >>> Stream.fold Fold.toList $ s1 `Stream.append` s2
105-- This operation can be used to fold an infinite lazy container of streams.
110append :: Stream m a -> Stream m a -> Stream m a
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