3-- Module : Streamly.Data.StreamK
4-- Copyright : (c) 2017 Composewell Technologies
7-- Maintainer : streamly@composewell.com
11-- Streams represented as chains of functions calls using Continuation Passing
12-- Style (CPS), suitable for dynamically composing potentially large number of
15-- Unlike the statically fused operations in "Streamly.Data.Stream", StreamK
16-- operations are less efficient, involving a function call overhead for each
17-- element, but they exhibit linear O(n) time complexity wrt to the number of
18-- stream compositions. Therefore, they are suitable for dynamically composing
19-- streams e.g. appending potentially infinite streams in recursive loops.
20-- While fused streams can be used to efficiently process elements as small as
21-- a single byte, CPS streams are typically used on bigger chunks of data to
22-- avoid the larger overhead per element. For more details See the @Stream vs
23-- StreamK@ section in the "Streamly.Data.Stream" module.
25-- In addition to the combinators in this module, you can use operations from
26-- "Streamly.Data.Stream" for StreamK as well by converting StreamK to Stream
27-- ('toStream'), and vice-versa ('fromStream'). Please refer to
28-- "Streamly.Internal.Data.StreamK" for more functions that have not yet been
31-- For documentation see the corresponding combinators in
32-- "Streamly.Data.Stream". Documentation has been omitted in this module unless
33-- there is a difference worth mentioning or if the combinator does not exist
34-- in "Streamly.Data.Stream".
38-- primitive/open loop operations that can be used recursively e.g. uncons,
39-- foldBreak, parseBreak should not be converted from StreamD for use in
40-- StreamK, instead native StreamK impl should be used.
42-- Closed loop operations like repeat, replicate, iterate etc can be converted
45-- In the last phase any operation like (toStreamK . f . toStreamD) should be
46-- rewritten to a K version of f.
47-- XXX Need to add rewrite rules for all missing StreamD operations.
49module Streamly.Data.StreamK
52 -- | To execute the code examples provided in this module in ghci, please
53 -- run the following commands first.
65 -- | Primitives to construct a stream from pure values or monadic actions.
66 -- All other stream construction and generation combinators described later
67 -- can be expressed in terms of these primitives. However, the special
68 -- versions provided in this module can be much more efficient in some
69 -- cases. Users can create custom combinators using these primitives.
80 -- | Please note that 'Stream' type does not observe any exceptions from
81 -- the consumer of the stream whereas 'StreamK' does.
108 -- * Combining Two Streams
109 -- | Unlike the operations in "Streamly.Data.Stream", these operations can
110 -- be used to dynamically compose large number of streams e.g. using the
111 -- 'concatMapWith' and 'mergeMapWith' operations. They have a linear O(n)
112 -- time complexity wrt to the number of streams being composed.
129 -- XXX is "bind/concatFor" better to have than crossWith?
130 -- crossWith f xs1 xs2 = concatFor xs1 (\x -> fmap (f x) xs2)
134 -- , CrossStreamK (..)
136 -- * Stream of streams
137 -- | Some useful idioms:
139 -- >>> concatFoldableWith f = Prelude.foldr f StreamK.nil
140 -- >>> concatMapFoldableWith f g = Prelude.foldr (f . g) StreamK.nil
141 -- >>> concatForFoldableWith f xs g = Prelude.foldr (f . g) StreamK.nil xs
148 -- * Buffered Operations
153 -- | Please note that 'Stream' type does not observe any exceptions from
154 -- the consumer of the stream whereas 'StreamK' does.
157 -- * Resource Management
158 -- | Please note that 'Stream' type does not observe any exceptions from
159 -- the consumer of the stream whereas 'StreamK' does.
164import Streamly.Internal.Data.StreamK
165import Prelude hiding (reverse, zipWith, mapM, dropWhile, take)
167#include "DocTestDataStreamK.hs"
171-- Continuation passing style (CPS) stream implementation. The 'K' in 'StreamK'
172-- stands for Kontinuation.
174-- StreamK can be constructed like lists, except that they use 'nil' instead of
175-- '[]' and 'cons' instead of ':'.
177-- `cons` adds a pure value at the head of the stream:
179-- >>> import Streamly.Data.StreamK (StreamK, cons, consM, nil)
180-- >>> stream = 1 `cons` 2 `cons` nil :: StreamK IO Int
182-- You can use operations from "Streamly.Data.Stream" for StreamK as well by
183-- converting StreamK to Stream ('toStream'), and vice-versa ('fromStream').
185-- >>> Stream.fold Fold.toList $ StreamK.toStream stream -- IO [Int]
188-- `consM` adds an effect at the head of the stream:
190-- >>> stream = effect 1 `consM` effect 2 `consM` nil
191-- >>> Stream.fold Fold.toList $ StreamK.toStream stream
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