1{-# OPTIONS_GHC -fno-warn-unused-imports #-}
3-- Module : User.Tutorials.Tutorial
4-- Copyright : (c) 2017 Composewell Technologies
7-- Maintainer : streamly@composewell.com
9-- THIS TUTORIAL IS OBSOLETE.
11-- In this tutorial we will show how Streamly can be used for idiomatic
12-- dataflow programming. Before you go through this tutorial we recommend that
13-- you take a look at the Streamly Getting Started guide so that you are ready
14-- to try out the examples.
16module User.Tutorials.Tutorial
24 -- * Imports and Supporting Code
27 -- * Generating Streams
30 -- * Eliminating Streams
33 -- * Transforming Streams
41 -- *** Deep Serial Composition ('Serial')
44 -- *** Wide Serial Composition ('WSerial')
56 -- *** Deep Serial Nesting ('Serial')
59 -- *** Wide Serial Nesting ('WSerial')
60 -- $interleavedNesting
77 -- * Where to go next?
82import Streamly.Data.Stream
84import Control.Applicative
86import Control.Monad.IO.Class (MonadIO(..))
87import Control.Monad.Trans.Class (MonadTrans (lift))
89-- CAUTION: please keep setup and imports sections in sync
91-- XXX This tutorial has to be re-written.
95-- In most of example snippets we do not repeat the imports. Where
96-- imports are not explicitly specified use the imports shown below.
99-- >>> :set -fno-warn-deprecations
100-- >>> import Data.Function ((&))
101-- >>> import Streamly.Prelude ((|:), (|&))
102-- >>> import Streamly.Data.Stream (Stream)
103-- >>> import Streamly.Internal.Data.Stream.Cross (CrossStream(..))
104-- >>> import qualified Streamly.Prelude as Stream
105-- >>> import qualified Streamly.Data.Fold as Fold
110-- The monadic stream API offered by Streamly is very close to the Haskell
111-- "Prelude" pure lists' API, it can be considered as a natural extension of
112-- lists to monadic actions. Streamly streams provide concurrent composition
113-- and merging of streams. It can be considered as a concurrent list
116-- The 'Serial' type is almost a drop in replacement for pure lists,
117-- pure lists are a special case of monadic streams. If you use 'nil' in place
118-- of '[]' and '|:' in place ':' you can replace a list with a 'Serial' stream.
119-- The only difference is that the elements must be monadic type and to operate
120-- on the streams we must use the corresponding functions from
121-- "Streamly.Prelude" instead of using the base "Prelude".
125-- 'nil' represents an empty stream and 'consM' or its operator form '|:' adds
126-- a monadic action at the head of the stream.
128-- >>> Stream.toList Stream.nil
131-- Stream.toList $ getLine |: getLine |: Stream.nil
136-- To create a singleton stream from a pure value use 'fromPure' and to
137-- create a singleton stream from a monadic action use 'fromEffect'.
139-- >>> Stream.toList $ Stream.fromPure 1
142-- Stream.toList $ Stream.fromEffect getLine
146-- To create a stream from pure values in a 'Foldable' container use
147-- 'fromFoldable' which is equivalent to a fold using 'cons' and 'nil':
149-- >>> Stream.toList $ Stream.fromFoldable [1..3]
152-- >>> Stream.toList $ Prelude.foldr Stream.cons Stream.nil [1..3]
155-- To create a stream from monadic actions in a 'Foldable' container just use a
156-- right fold using 'consM' and 'nil':
158-- >>> Stream.drain $ Prelude.foldr (|:) Stream.nil [putStr "Hello ", putStrLn "world!"]
161-- For more ways to construct a stream see the module "Streamly.Prelude".
165-- We have already seen 'drain' and toList to eliminate a stream in the
166-- examples above. 'drain' runs a stream discarding the results i.e. only
167-- for effects. 'toList' runs the stream and collects the results in a list.
169-- For other ways to eliminate a stream see the @Folding@ section in
170-- "Streamly.Prelude" module.
174-- Transformation over a stream is the equivalent of a @for@ loop construct in
175-- imperative paradigm. We iterate over every element in the stream and perform
176-- certain transformations for each element. Transformations may involve
177-- mapping functions over the elements, filtering elements from the stream or
178-- folding all the elements in the stream into a single value. Streamly streams
179-- are exactly like lists and you can perform all the transformations in the
180-- same way as you would on lists.
182-- Here is a simple console echo program that just echoes every input line,
187-- Stream.repeatM getLine
188-- & Stream.mapM putStrLn
192-- The following code snippet reads lines from standard input, filters blank
193-- lines, drops the first non-blank line, takes the next two, up cases them,
194-- numbers them and prints them:
196-- >>> import Data.Char (toUpper)
199-- Stream.repeatM getLine
200-- & Stream.filter (not . null)
203-- & fmap (map toUpper)
204-- & Stream.zipWith (\n s -> show n ++ " " ++ s) (Stream.fromFoldable [1..])
205-- & Stream.mapM putStrLn
212-- We can combine two streams into a single stream using semigroup composition
213-- operation '<>'. Streams can be combined in many different ways as described
214-- in the following sections, the '<>' operation behaves differently depending
215-- on the stream type in effect. The stream type and therefore the composition
216-- style can be changed at any point using one of the type combinators as
221-- The 'Semigroup' operation '<>' of the 'Serial' type combines the two streams
222-- in a /serial depth first/ manner. We use the 'fromSerial' type combinator to
223-- effect 'Serial' style of composition. We can also use an explicit 'Serial'
224-- type annotation for the stream to achieve the same effect. However, since
225-- 'Serial' is the default type unless explicitly specified by using a
226-- combinator, we can omit using an explicit combinator or type annotation for
227-- this style of composition.
229-- When two streams with multiple elements are combined in this manner, the
230-- monadic actions in the two streams are performed sequentially i.e. first all
231-- actions in the first stream are performed sequentially and then all actions
232-- in the second stream are performed sequentially. We call it
233-- /serial depth first/ as the full depth of one stream is fully traversed
234-- before we move to the next. The following example prints the sequence 1, 2,
237-- >>> stream1 = print 1 |: print 2 |: Stream.nil
238-- >>> stream2 = print 3 |: print 4 |: Stream.nil
239-- >>> Stream.drain $ stream1 <> stream2
245-- All actions in both the streams are performed serially in the same thread.
247-- The polymorphic version of the binary operation '<>' of the 'Serial' type is
248-- 'serial'. We can use 'serial' to join streams in a sequential manner
249-- irrespective of the type of stream:
251-- >>> Stream.drain $ stream1 `Stream.serial` stream2
260-- The 'Semigroup' operation '<>' of the 'WSerial' type combines the two
261-- streams in a /serial breadth first/ manner. We use the fromWSerial type
262-- combinator to effect 'WSerial' style of composition. We can also use the
263-- 'WSerial' type annotation for the stream to achieve the same effect.
265-- When two streams with multiple elements are combined in this manner, we
266-- traverse all the streams in a breadth first manner i.e. one action from each
267-- stream is performed and yielded to the resulting stream before we come back
268-- to the first stream again and so on.
269-- The following example prints the sequence 1, 3, 2, 4
271-- >>> stream1 = print 1 |: print 2 |: Stream.nil
272-- >>> stream2 = print 3 |: print 4 |: Stream.nil
273-- >>> Stream.drain $ Stream.fromWSerial $ stream1 <> stream2
279-- Even though the monadic actions of the two streams are performed in an
280-- interleaved manner they are all performed serially in the same thread.
282-- The polymorphic version of the 'WSerial' binary operation '<>' is called
283-- 'wSerial'. We can use 'wSerial' to join streams in an interleaved manner
284-- irrespective of the type, notice that we have not used the fromWSerial
285-- combinator in the following example:
287-- >>> Stream.drain $ stream1 `Stream.wSerial` stream2
293-- Note that this composition cannot be used to fold infinite number of streams
294-- since it requires preserving the state until a stream is finished.
298-- We can use 'Monoid' instances to fold a container of streams in the desired
299-- style using 'fold' or 'foldMap'. We have also provided some fold utilities
300-- to fold streams using the polymorphic combine operations:
302-- * 'concatFoldableWith' is like 'fold', it folds a 'Foldable' container of
303-- streams using the given composition operator.
304-- * 'concatMapFoldableWith' is like 'foldMap', it folds like
305-- @concatFoldableWith@ but also maps a function before folding.
306-- * 'concatForFoldableWith' is like @concatMapFoldableWith@ but the container
307-- argument comes before the function argument.
309-- All of the following are equivalent:
312-- traced = Stream.fromEffect . print
314-- Stream.drain $ foldMap traced [1..10]
315-- Stream.drain $ Stream.concatFoldableWith Stream.serial (map traced [1..10])
316-- Stream.drain $ Stream.concatMapFoldableWith Stream.serial traced [1..10]
317-- Stream.drain $ Stream.concatForFoldableWith Stream.serial [1..10] traced
323-- Till now we discussed ways to apply transformations on a stream or to merge
324-- streams together to create another stream. We mentioned earlier that
325-- transforming a stream is similar to a @for@ loop in the imperative paradigm.
326-- We will now discuss the concept of a nested composition of streams which is
327-- analogous to nested @for@ loops in the imperative paradigm. Functional
328-- programmers call this style of composition a list transformer or @ListT@.
329-- Logic programmers call it a logic monad or non-deterministic composition,
330-- but for ordinary imperative minded people like me it is easier to think in
331-- terms of good old nested @for@ loops.
335-- In functional programmer's parlance the 'Monad' instances of different
336-- 'IsStream' types implement non-determinism, exploring all possible
337-- combination of choices from both the streams. From an imperative
338-- programmer's point of view it behaves like nested loops i.e. for each
339-- element in the first stream and for each element in the second stream
340-- execute the body of the loop.
342-- The 'Monad' instances of 'Serial', 'WSerial', 'Async' and 'WAsync'
343-- stream types support different flavors of nested looping. In other words,
344-- they are all variants of list transformer. The nesting behavior of these
345-- types correspond exactly to the way they merge streams as we discussed in
346-- the previous section.
351-- The 'Monad' composition of the 'CrossStream' type behaves like a standard list
354-- Let's start with an example with a simple @for@ loop without any nesting.
355-- For simplicity of illustration we are using streams of pure values in all
356-- the examples. However, the streams could also be made of monadic actions
360-- Stream.drain $ unCrossStream $ do
361-- x <- CrossStream (Stream.fromFoldable [3,2,1])
362-- CrossStream (Stream.fromEffect $ print x)
368-- As we can see, the code after the @fromFoldable@ statement is run three
369-- times, once for each value of @x@ drawn from the stream. All the three
370-- iterations are serial and run in the same thread one after another. In
371-- imperative terms this is equivalent to a @for@ loop with three iterations.
373-- We can write the console echo program that we wrote earlier using the monad
378-- Stream.drain $ unCrossStream $ do
379-- x <- CrossStream (Stream.repeatM getLine)
380-- CrossStream (Stream.fromEffect $ putStrLn x)
383-- When multiple streams are composed using this style they nest in a DFS
387-- Stream.drain $ unCrossStream $ do
388-- x <- CrossStream (Stream.fromFoldable [1,2])
389-- y <- CrossStream (Stream.fromFoldable [3,4])
390-- CrossStream (Stream.fromEffect $ print (x, y))
397-- i.e. inner loop iterations ((1,3), (1,4)) are executed before we proceed to
398-- the next iteration of the outer loop ((2,3), (2,4)). This behaves just like
399-- nested @for@ loops in imperative programming.
401-- Notice that this is analogous to merging streams of type 'Serial' or merging
402-- streams using 'serial'.
404-- $interleavedNesting
406-- TBD: generate the WSerial type using the TH utils.
408-- The 'Monad' composition of 'WSerial' type interleaves the iterations of
409-- outer and inner loops in a nested loop composition.
412-- Stream.drain $ getWSerial $ do
413-- x <- WSerialStream (Stream.fromFoldable [1,2])
414-- y <- WSerialStream (Stream.fromFoldable [3,4])
415-- WSerialStream (Stream.fromEffect $ print (x, y))
422-- Note that (2,3) is preferred to (1,4). This works exactly the same way as
423-- the merging of two streams using 'wSerial' works.
425-- The fromWSerial type combinator can be used to switch to this style of
426-- composition. Alternatively, a type annotation can be used to specify the
427-- type of the stream as 'WSerial'.
433-- composed :: Stream IO ()
434-- composed = unCrossStream $ do
435-- sz <- CrossStream sizes
436-- cl <- CrossStream colors
437-- sh <- CrossStream shapes
438-- CrossStream (Stream.fromEffect $ print (sz, cl, sh))
440-- sizes = Stream.fromFoldable [1, 2, 3]
441-- colors = Stream.fromFoldable ["red", "green", "blue"]
442-- shapes = Stream.fromFoldable ["triangle", "square", "circle"]
448-- 'fmap' transforms a stream by mapping a function on all elements of the
449-- stream. 'fmap' behaves in the same way for all stream types, it is always
452-- >>> (Stream.toList $ fmap show $ Stream.fromFoldable [1..10]) >>= print
453-- ["1","2","3","4","5","6","7","8","9","10"]
455-- Also see functions 'mapM' and 'sequence' from "Streamly.Prelude" module
456-- which can map actions concurrently depending on the type of the input stream.
460-- Applicative is precisely the same as the 'ap' operation of 'Monad'. For
461-- zipping applicatives separate types 'ZipStream' and 'ZipAsync' are
464-- The following is an example of 'CrossStream' applicative, it runs all iterations
467-- >>> p n = Stream.fromEffect (print n >> pure n)
468-- >>> s1 = p 1 <> p 2
469-- >>> s2 = p 3 <> p 4
471-- >>> (Stream.toList $ unCrossStream $ (,) <$> CrossStream s1 <*> CrossStream s2) >>= print
478-- [(1,3),(1,4),(2,3),(2,4)]
480-- Similarly, 'WSerial' applicative runs the iterations in an interleaved
481-- order but being serial it too takes a total of 17 seconds:
483-- >> (Stream.toList $ Stream.WSerialStream $ (,) <$> WSerialStream s1 <*> WSerialStream s2) >>= print
490-- [(1,3),(2,3),(1,4),(2,4)]
494-- Zipping is a special transformation where the corresponding elements of two
495-- streams are combined together using a zip function producing a new stream of
496-- outputs. Two different types are provided for serial and concurrent zipping.
497-- These types provide an applicative instance that can be used to lift
498-- functions to zip the argument streams.
499-- Also see the zipping functions in the "Streamly.Prelude" module.
503-- The applicative instance of 'ZipSerial' type zips streams serially.
504-- 'fromZipSerial' type combinator can be used to switch to serial applicative
507-- >>> p n = Stream.fromEffect (print n >> pure n)
508-- >>> s1 = Stream.fromSerial $ p 1 <> p 2
509-- >>> s2 = Stream.fromSerial $ p 3 <> p 4
510-- >>> (Stream.toList $ Stream.fromZipSerial $ (,) <$> s1 <*> s2) >>= print
520-- * Read the concurrent streams tutorial
521-- * See the examples in <https://github.com/composewell/streamly-examples streamly-examples> repo.
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