1# Frequently Asked Questions
3This document provides idioms or examples to solve common programming
4problems using streamly. To start with please go through [Streamly Quick
5Overview](/docs/User/Tutorials/quick-overview.md) and [`Streamly examples repository`][streamly-examples].
6This document provides additional examples.
8## Distribute and Zip Concurrently
10Transform a stream in multiple ways, generating multiple transformed
11streams and then zip the corresponding elements of each resulting stream
12together to create a single transformed stream.
14Distributing a value to a stream of consumers concurrently:
17{-# LANGUAGE FlexibleContexts #-}
19import Data.Function ((&))
20import qualified Streamly.Data.Fold as Fold
21import qualified Streamly.Data.Stream.Prelude as Stream
24 Stream.fromList [return . (+ 1), return . (+ 2)] -- Stream of functions
25 & fmap ($ x) -- Stream of lazy actions
26 & Stream.parSequence (Stream.ordered True) -- Evaluate concurrently
27 & Stream.fold Fold.toList -- Fold to list
30Use `parApply` to zip streams concurrently. Here, we zip three singleton
36 let app = Stream.parApply id
38 `fmap` Stream.fromEffect (return $ show x)
39 `app` Stream.fromEffect (return $ x + 1)
40 `app` Stream.fromEffect (return $ fromIntegral x / 2)
41 & Stream.fold Fold.one
44Applying a function concurrently to your input stream:
49 & Stream.parMapM (Stream.ordered True) f
50 & Stream.fold Fold.toList
53You can now use the concurrent map to pipe each element through multiple
54transformations using the distribute/zip operations.
57>>> g f1 [1,2,3,4::Int]
58[[2,3],[3,4],[4,5],[5,6]]
60>>> g f2 [1,2,3,4::Int]
61[Just ("1",2,0.5),Just ("2",3,1.0),Just ("3",4,1.5),Just ("4",5,2.0)]
64Instead of using `parApply` directly, you can use `mkZipType` to
65create a zip Applicative newtype so that you can use the `Applicative`
69{-# LANGUAGE UndecidableInstances #-}
70{-# LANGUAGE FlexibleInstances #-}
71{-# LANGUAGE MultiParamTypeClasses #-}
72{-# LANGUAGE TemplateHaskell #-}
74import Streamly.Internal.Data.Stream.TypeGen
77$(mkZippingType "ZipConcurrent" "app" True)
82The `writeLastN` fold can be used to create a stream of sliding windows.
85>>> import qualified Streamly.Data.Array as Array
87 Stream.fromList [1,2,3,4,5::Int]
88& Stream.scan (Array.writeLastN 2)
89& Stream.fold Fold.toList
91[fromList [],fromList [1],fromList [1,2],fromList [2,3],fromList [3,4],fromList [4,5]]
94Also see the "Streamly.Internal.Data.Fold.Window" module for widnow based folds.
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