h$t      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ Safe-Inferred35678n streamingA left-strict pair; the base functor for streams of individual elements.( streaming) streaming5 Safe-Inferred '(>;q+; streaming-Map a stream to its church encoding; compare Data.List.foldr. X may be more efficient in some cases when applicable, but it is less safe.  destroy s construct eff done = eff . iterT (return . construct . fmap eff) . fmap done $ s < streaming< reorders the arguments of ; to be more akin to foldr It is more convenient to query in ghci to figure out what kind of 'algebra' you need to write.:t streamFold return join(Monad m, Functor f) =>< (f (m a) -> m a) -> Stream f m a -> m a -- iterT":t streamFold return (join . lift)2(Monad m, Monad (t m), Functor f, MonadTrans t) =>= (f (t m a) -> t m a) -> Stream f m a -> t m a -- iterTM:t streamFold return effect"(Monad m, Functor f, Functor g) => (f (Stream g m r) -> Stream g m r) -> Stream f m r -> Stream g m r,:t \f -> streamFold return effect (wrap . f)"(Monad m, Functor f, Functor g) =>+ (f (Stream g m a) -> g (Stream g m a))< -> Stream f m a -> Stream g m a -- maps::t \f -> streamFold return effect (effect . fmap wrap . f)"(Monad m, Functor f, Functor g) =>/ (f (Stream g m a) -> m (g (Stream g m a)))> -> Stream f m a -> Stream g m a -- mapped  streamFold done eff construct = eff . iterT (return . construct . fmap eff) . fmap done = streaming%Reflect a church-encoded stream; cp. GHC.Exts.build streamFold return_ effect_ step_ (streamBuild psi) = psi return_ effect_ step_> streamingInspect the first stage of a freely layered sequence. Compare  Pipes.next and the replica Streaming.Prelude.next. This is the uncons for the general ?. unfold inspect = id Streaming.Prelude.unfoldr StreamingPrelude.next = id? streamingBuild a Stream by unfolding steps starting from a seed. See also the specialized  in the prelude. unfold inspect = id -- modulo the quotient we work with unfold Pipes.next :: Monad m => Producer a m r -> Stream ((,) a) m r unfold (curry (:>) . Pipes.next) :: Monad m => Producer a m r -> Stream (Of a) m r@ streamingMap layers of one functor to another with a transformation. Compare hoist, which has a similar effect on the monadic parameter. +maps id = id maps f . maps g = maps (f . g)A streamingMap layers of one functor to another with a transformation involving the base monad. @ is more fundamental than mapsM, which is best understood as a convenience for effecting this frequent composition: ,mapsM phi = decompose . maps (Compose . phi)The streaming prelude exports the same function under the better name mapped., which overlaps with the lens libraries.B streamingMap layers of one functor to another with a transformation. Compare hoist, which has a similar effect on the monadic parameter. mapsPost id = id mapsPost f . mapsPost g = mapsPost (f . g) mapsPost f = maps fmapsPost is essentially the same as @, but it imposes a  constraint on its target functor rather than its source functor. It should be preferred if  is cheaper for the target functor than for the source functor.C streamingMap layers of one functor to another with a transformation involving the base monad.  mapsMPost is essentially the same as A, but it imposes a  constraint on its target functor rather than its source functor. It should be preferred if  is cheaper for the target functor than for the source functor.mapsPost is more fundamental than  mapsMPost, which is best understood as a convenience for effecting this frequent composition: 4mapsMPost phi = decompose . mapsPost (Compose . phi)The streaming prelude exports the same function under the better name  mappedPost., which overlaps with the lens libraries.D streaming-Rearrange a succession of layers of the form Compose m (f x).we could as well define  decompose by mapsM: decompose = mapped getComposebut mapped is best understood as: -mapped phi = decompose . maps (Compose . phi)since maps and hoist are the really fundamental operations that preserve the shape of the stream: maps :: (Monad m, Functor f) => (forall x. f x -> g x) -> Stream f m r -> Stream g m r hoist :: (Monad m, Functor f) => (forall a. m a -> n a) -> Stream f m r -> Stream f n rE streaming7Run the effects in a stream that merely layers effects.F streaming.Map each layer to an effect, and run them all.G streaming=Interpolate a layer at each segment. This specializes to e.g. intercalates :: (Monad m, Functor f) => Stream f m () -> Stream (Stream f m) m r -> Stream f m rH streaming(Specialized fold following the usage of Control.Monad.Trans.Free iterTM alg = streamFold return (join . lift) iterTM alg = iterT alg . hoist liftI streaming(Specialized fold following the usage of Control.Monad.Trans.Free iterT alg = streamFold return join alg iterT alg = runIdentityT . iterTM (IdentityT . alg . fmap runIdentityT)J streaming*Dissolves the segmentation into layers of  Stream f m layers.K streamingSplit a succession of layers after some number, returning a streaming or effectful pair.+rest <- S.print $ S.splitAt 1 $ each [1..3]1 S.print rest23 :splitAt 0 = return splitAt n >=> splitAt m = splitAt (m+n) Thus, e.g.9rest <- S.print $ splitsAt 2 >=> splitsAt 2 $ each [1..5]1234 S.print rest5M streaming=Break a stream into substreams each with n functorial layers.6S.print $ mapped S.sum $ chunksOf 2 $ each [1,1,1,1,1]221N streaming;Make it possible to 'run' the underlying transformed monad.O streamingRepeat a functorial layer (a "command" or "instruction") forever.P streamingRepeat an effect containing a functorial layer, command or instruction forever.Q streamingRepeat a functorial layer, command or instruction a fixed number of times.  replicates n = takes n . repeatsR streaming4Construct an infinite stream by cycling a finite one cycles = foreverS streamingA less-efficient version of  that works properly even when its argument is not a monad morphism. "hoistUnexposed = hoist . unexposedT streaming The same as , but explicitly named to indicate that it is not entirely safe. In particular, its argument must be a monad morphism.U streaming The same as T , but with a  constraint on the target rather than the source. This must be used only with a monad morphism.X streaming6Map a stream directly to its church encoding; compare Data.List.foldr It permits distinctions that should be hidden, as can be seen from e.g. isPure stream = destroyExposed (const True) (const False) (const True)>and similar nonsense. The crucial constraint is that the m x -> x argument is an Eilenberg-Moore algebra. See Atkey, "Reasoning about Stream Processing with Effects"When in doubt, use ; instead.Y streamingThis is akin to the observe of Pipes.Internal1 . It reeffects the layering in instances of  Stream f m r# so that it replicates that of FreeT.Z streaming&Wrap a new layer of a stream. So, e.g. S.cons :: Monad m => a -> Stream (Of a) m r -> Stream (Of a) m r S.cons a str = wrap (a :> str)and, recursively: S.each :: (Monad m, Foldable t) => t a -> Stream (Of a) m () S.each = foldr (\a b -> wrap (a :> b)) (return ())The two operations wrap :: (Monad m, Functor f ) => f (Stream f m r) -> Stream f m r effect :: (Monad m, Functor f ) => m (Stream f m r) -> Stream f m r7are fundamental. We can define the parallel operations yields and lift in terms of them yields :: (Monad m, Functor f ) => f r -> Stream f m r yields = wrap . fmap return lift :: (Monad m, Functor f ) => m r -> Stream f m r lift = effect . fmap return[ streaming$Wrap an effect that returns a stream effect = join . lift\ streamingyields is like lift for items in the streamed functor. It makes a singleton or one-layer succession. lift :: (Monad m, Functor f) => m r -> Stream f m r yields :: (Monad m, Functor f) => f r -> Stream f m rViewed in another light, it is like a functor-general version of yield: S.yield a = yields (a :> ())] streamingZip two streams together. The ^8 function should generally be preferred for efficiency.^ streamingZip two streams together.` streamingInterleave functor layers, with the effects of the first preceding the effects of the second. When the first stream runs out, any remaining effects in the second are ignored. #interleaves = zipsWith (liftA2 (,))let paste = \a b -> interleaves (Q.lines a) (maps (Q.cons' '\t') (Q.lines b))Q.stdout $ Q.unlines $ paste "hello\nworld\n" "goodbye\nworld\n" hello goodbye world worlda streaming0Swap the order of functors in a sum of functors.S.toList $ S.print $ separate $ maps S.switch $ maps (S.distinguish (=='a')) $ S.each "banana"'a''a''a' "bnn" :> ()S.toList $ S.print $ separate $ maps (S.distinguish (=='a')) $ S.each "banana"'b''n''n' "aaa" :> ()b streamingGiven a stream on a sum of functors, make it a stream on the left functor, with the streaming on the other functor as the governing monad. This is useful for acting on one or the other functor with a fold, leaving the other material for another treatment. It generalizes  , but actually streams properly.let odd_even = S.maps (S.distinguish even) $ S.each [1..10::Int]:t separate odd_evenseparate odd_even6 :: Monad m => Stream (Of Int) (Stream (Of Int) m) ()Now, for example, it is convenient to fold on the left and right values separately:'S.toList $ S.toList $ separate odd_even#[2,4,6,8,10] :> ([1,3,5,7,9] :> ())3Or we can write them to separate files or whatever:S.writeFile "even.txt" . S.show $ S.writeFile "odd.txt" . S.show $ S.separate odd_even:! cat even.txt246810:! cat odd.txt13579"Of course, in the special case of Stream (Of a) m r;, we can achieve the above effects more simply by using S.toList . S.filter even $ S.toList . S.filter odd $ S.copy $ each [1..10::Int]#[2,4,6,8,10] :> ([1,3,5,7,9] :> ())But b and c are functor-general.d streamingIf Of had a Comonad instance, then we'd have copy = expand extendSee e for a version that requires a  Functor g instance instead.e streamingIf Of had a Comonad instance, then we'd have copy = expandPost extendSee d for a version that requires a  Functor f instance instead.g streamingGroup layers in an alternating stream into adjoining sub-streams of one type or another.h streamingh interleaves the pure applicative action with the return of the monad forever. It is the  of the  instance, thus never <|> a = a a <|> never = a!and so on. If w is a monoid then never :: Stream (Of w) m r" is the infinite sequence of  , and  str1 <|> str2 appends the elements monoidally until one of streams ends. Thus we have, e.g.S.stdoutLn $ S.take 2 $ S.stdinLn <|> S.repeat " " <|> S.stdinLn <|> S.repeat " " <|> S.stdinLn1231 2 34564 5 6This is equivalent toS.stdoutLn $ S.take 2 $ foldr (<|>) never [S.stdinLn, S.repeat " ", S.stdinLn, S.repeat " ", S.stdinLn ]Where f is a monad, (<|>) sequences the conjoined streams stepwise. See the definition of paste  5https://gist.github.com/michaelt/6c6843e6dd8030e95d58here, where the separate steps are bytestreams corresponding to the lines of a file. Given, say, data Branch r = Branch r r deriving Functor -- add obvious applicative instancethen !never :: Stream Branch Identity r; is the pure infinite binary tree with (inaccessible) r)s in its leaves. Given two binary trees, tree1 <|> tree2 intersects them, preserving the leaves that came first, so tree1 <|> never = tree1Stream Identity m r is an action in m that is indefinitely delayed. Such an action can be constructed with e.g. j. untilJust :: (Monad m, Applicative f) => m (Maybe r) -> Stream f m rGiven two such items, <|> instance races them. It is thus the iterative monad transformer specially defined in https://hackage.haskell.org/package/free-4.12.1/docs/Control-Monad-Trans-Iter.htmlControl.Monad.Trans.IterSo, for example, we might writelet justFour str = if length str == 4 then Just str else Nothing,let four = untilJust (fmap justFour getLine)run four one two three four"four"The  instance in  https://hackage.haskell.org/package/free-4.12.1/docs/Control-Monad-Trans-Free.htmlControl.Monad.Trans.Free is avowedly wrong, though no explanation is given for this.j streamingRepeat av streamingThe * instance glues streams together stepwise. +empty = never (<|>) = zipsWith (liftA2 (,)) See also h, j and iz streaming?Operates covariantly on the stream result, not on its elements: Stream (Of a) m r ^ ^ | `--- This is what  and  use `--- This is what functions like S.map/S.zipWith use 5789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijk5789:?QOP[Z\=RihjGJIH;<>@ABCSDFENgMKLk]^_f`bcdeaYTUVWX Safe-Inferred j streaming Note that , , , and  are all so-called natural transformations on the primitive Of a functor. If we write % type f ~~> g = forall x . f x -> g x*then we can restate some types as follows:  mapOf :: (a -> b) -> Of a ~~> Of b -- Bifunctor first lazily :: Of a ~~> (,) a Identity . fst' :: Of a ~~> Identity aManipulation of a  Stream f m r by mapping often turns on recognizing natural transformations of f . Thus maps is far more general the the map of the Streaming.Prelude, which can be defined thus:  S.map :: (a -> b) -> Stream (Of a) m r -> Stream (Of b) m r S.map f = maps (mapOf f)i.e. ) S.map f = maps (\(a :> x) -> (f a :> x))This rests on recognizing that mapOf is a natural transformation; note though that it results in such a transformation as well: 9 S.map :: (a -> b) -> Stream (Of a) m ~~> Stream (Of b) m Thus we can maps it in turn. streaming9Convert a standard Haskell pair into a left-strict pair  streamingfst' and snd'/ extract the first and second element of a pairS.fst' (1:>"hi")1S.snd' (1:>"hi")"hi"They are contained in the _first and _second- lenses, if any lens library is in scopeimport Lens.Micro(1:>"hi") ^. S._first1(1:>"hi") ^. S._second"hi" streaming,Map a function over the first element of an Of pairS.mapOf even (1:>"hi") False :> "hi"mapOf is just first from the  Bifunctor instancefirst even (1:>"hi") False :> "hi"and is contained in the _first lensimport Lens.Microover S._first even (1:>"hi") False :> "hi" streaming4A lens into the first element of a left-strict pair  streaming5A lens into the second element of a left-strict pair  streamingBreak a sequence upon meeting element falls under a predicate, keeping it and the rest of the stream as the return value./rest <- S.print $ S.break even $ each [1,1,2,3]11 S.print rest23 streamingYield elements, using a fold to maintain state, until the accumulated value satifies the supplied predicate. The fold will then be short-circuited and the element that breaks it will be put after the break. This function is easiest to use with  rest <- each [1..10] & L.purely S.breakWhen L.sum (>10) & S.print1234 S.print rest5678910 streamingBreak during periods where the predicate is not satisfied, grouping the periods when it is.S.print $ mapped S.toList $ S.breaks not $ S.each [False,True,True,False,True,True,False] [True,True] [True,True]S.print $ mapped S.toList $ S.breaks id $ S.each [False,True,True,False,True,True,False][False][False][False] streamingApply an action to all values, re-yielding each. The return value (y) of the function is ignored.1S.product $ S.chain Prelude.print $ S.each [1..5]12345 120 :> () See also  for a variant of this which uses the return value of the function to transorm the values in the stream. streamingMake a stream of foldable containers into a stream of their separate elements. This is just concat str = for str each$S.print $ S.concat (each ["xy","z"])'x''y''z'$Note that it also has the effect of  ,   map snd and such-like operations.5S.print $ S.concat $ S.each [Just 1, Nothing, Just 2]12>S.print $ S.concat $ S.each [Right 1, Left "Error!", Right 2]12.S.print $ S.concat $ S.each [('A',1), ('B',2)]12 streaming The natural cons for a  Stream (Of a). !cons a stream = yield a >> streamUseful for interoperation: Data.Text.foldr S.cons (return ()) :: Text -> Stream (Of Char) m () Lazy.foldrChunks S.cons (return ()) :: Lazy.ByteString -> Stream (Of Strict.ByteString) m () and so on. streaming1Cycle repeatedly through the layers of a stream, ad inf.& This function is functor-general cycle = foreverrest <- S.print $ S.splitAt 3 $ S.cycle (yield True >> yield False)TrueFalseTrueS.print $ S.take 3 restFalseTrueFalse streaming0Interpolate a delay of n seconds between yields. streamingWhere a transformer returns a stream, run the effects of the stream, keeping the return value. This is usually used at the type drained :: Monad m => Stream (Of a) m (Stream (Of b) m r) -> Stream (Of a) m r drained = join . fmap (lift . effects)Here, for example, we split a stream in two places and throw out the middle segment:rest <- S.print $ S.drained $ S.splitAt 2 $ S.splitAt 5 $ each [1..7]12 S.print rest67*In particular, we can define versions of take and  takeWhile which retrieve the return value of the rest of the stream - and which can thus be used with @: take' n = S.drained . S.splitAt n takeWhile' thus = S.drained . S.span thus streamingIgnore the first n elements of a stream, but carry out the actions,S.toList $ S.drop 2 $ S.replicateM 5 getLineabcde["c","d","e"] :> ()+Because it retains the final return value, drop n" is a suitable argument for maps:S.toList $ concats $ maps (S.drop 4) $ chunksOf 5 $ each [1..20][5,10,15,20] :> () streamingIgnore elements of a stream until a test succeeds, retaining the rest.0S.print $ S.dropWhile ((< 5) . length) S.stdinLn one two three"three" four"four"^CInterrupted. streaming2Stream the elements of a pure, foldable container.S.print $ each [1..3]123 streamingReduce a stream, performing its actions but ignoring its elements.-rest <- S.effects $ S.splitAt 2 $ each [1..5] S.print rest345$ should be understood together with  and is subject to the rules ;S.effects . S.copy = id hoist S.effects . S.copy = id The similar effects and copy operations in Data.ByteString.Streaming obey the same rules. streamingBefore evaluating the monadic action returning the next step in the 7,  wrapEffect1 extracts the value in a monadic computation m a and passes it to a computation a -> m y. streaming*Exhaust a stream remembering only whether a was an element. streamingAn infinite stream of enumerable values, starting from a given value. It is the same as S.iterate succ0. Because their return type is polymorphic, enumFrom,  enumFromThen and iterate are useful with functions like zip and zipWith, which require the zipped streams to have the same return type. For example, with  each [1..]; the following bit of connect-and-resume would not compile:rest <- S.print $ S.zip (S.enumFrom 1) $ S.splitAt 3 $ S.each ['a'..'z'](1,'a')(2,'b')(3,'c')S.print $ S.take 3 rest'd''e''f' streamingAn infinite sequence of enumerable values at a fixed distance, determined by the first and second values. See the discussion of +S.print $ S.take 3 $ S.enumFromThen 100 200100200300 streamingRemove the elements from a stream of values, retaining the structure of layers. streaming/Skip elements of a stream that fail a predicate streaming2Skip elements of a stream that fail a monadic test streamingStrict fold of a 7 of elements, preserving only the result of the fold, not the return value of the stream. The third parameter will often be % where a fold is written by hand:S.fold_ (+) 0 id $ each [1..10]55It can be used to replace a standard Haskell type with one more suited to writing a strict accumulation function. It is also crucial to the Applicative instance for Control.Foldl.Fold Control.Foldl.purely fold :: Monad m => Fold a b -> Stream (Of a) m () -> m b streamingStrict fold of a 7 of elements that preserves the return value. The third parameter will often be ! where a fold is written by hand:S.fold (+) 0 id $ each [1..10]55 :> ()9S.fold (*) 1 id $ S.fold (+) 0 id $ S.copy $ each [1..10]3628800 :> (55 :> ())It can be used to replace a standard Haskell type with one more suited to writing a strict accumulation function. It is also crucial to the Applicative instance for Control.Foldl.Fold We can apply such a fold purely Control.Foldl.purely S.fold :: Monad m => Fold a b -> Stream (Of a) m r -> m (Of b r)Thus, specializing a bit: L.purely S.fold L.sum :: Stream (Of Int) Int r -> m (Of Int r) mapped (L.purely S.fold L.sum) :: Stream (Stream (Of Int)) IO r -> Stream (Of Int) IO r)Here we use the Applicative instance for Control.Foldl.Fold to stream three-item segments of a stream together with their sums and products.S.print $ mapped (L.purely S.fold (liftA3 (,,) L.list L.product L.sum)) $ chunksOf 3 $ each [1..10] ([1,2,3],6,6)([4,5,6],120,15)([7,8,9],504,24) ([10],10,10) streaming*Strict, monadic fold of the elements of a  Stream (Of a) Control.Foldl.impurely foldM :: Monad m => FoldM a b -> Stream (Of a) m () -> m b streaming*Strict, monadic fold of the elements of a  Stream (Of a) Control.Foldl.impurely foldM' :: Monad m => FoldM a b -> Stream (Of a) m r -> m (b, r)Thus to accumulate the elements of a stream as a vector, together with a random element we might write:L.impurely S.foldM (liftA2 (,) L.vectorM L.random) $ each [1..10::Int] :: IO (Of (Vector Int, Maybe Int) ())%([1,2,3,4,5,6,7,8,9,10],Just 9) :> () streamingA natural right fold for consuming a stream of elements. See also the more general H in the  Streaming' module and the still more general ; foldrT (\a p -> Streaming.yield a >> p) = id foldrT (\a p -> Pipes.yield a >> p) :: Monad m => Stream (Of a) m r -> Producer a m r foldrT (\a p -> Conduit.yield a >> p) :: Monad m => Stream (Of a) m r -> Conduit a m r streamingA natural right fold for consuming a stream of elements. See also the more general I in the  Streaming' module and the still more general ; streamingfor replaces each element of a stream with an associated stream. Note that the associated stream may layer any functor. streamingGroup elements of a stream in accordance with the supplied comparison.S.print $ mapped S.toList $ S.groupBy (>=) $ each [1,2,3,1,2,3,4,3,2,4,5,6,7,6,5][1][2] [3,1,2,3] [4,3,2,4][5][6][7,6,5] streaming%Group successive equal items together5S.toList $ mapped S.toList $ S.group $ each "baaaaad"["b","aaaaa","d"] :> ()S.toList $ concats $ maps (S.drained . S.splitAt 1) $ S.group $ each "baaaaaaad" "bad" :> () streaming;Intersperse given value between each element of the stream.(S.print $ S.intersperse 0 $ each [1,2,3]10203 streamingIterate a pure function from a seed value, streaming the results forever streamingIterate a monadic function from a seed value, streaming the results forever streaming*Run a stream, remembering only its length:runIdentity $ S.length_ (S.each [1..10] :: Stream (Of Int) Identity ())10 streaming6Run a stream, keeping its length and its return value.7S.print $ mapped S.length $ chunksOf 3 $ S.each [1..10]3331 streaming)Standard map on the elements of a stream.6S.stdoutLn $ S.map reverse $ each (words "alpha beta")ahplaateb streamingReplace each element of a stream with the result of a monadic actionS.print $ S.mapM readIORef $ S.chain (\ior -> modifyIORef ior (*100)) $ S.mapM newIORef $ each [1..6]100200300400500600 See also  for a variant of this which ignores the return value of the function and just uses the side effects. streaming:Reduce a stream to its return value with a monadic action.#S.mapM_ Prelude.print $ each [1..3]123:rest <- S.mapM_ Prelude.print $ S.splitAt 3 $ each [1..10]123 S.sum rest49 :> () streamingMap layers of one functor to another with a transformation involving the base monad.This function is completely functor-general. It is often useful with the more concrete type mapped :: (forall x. Stream (Of a) IO x -> IO (Of b x)) -> Stream (Stream (Of a) IO) IO r -> Stream (Of b) IO r >to process groups which have been demarcated in an effectful, IO.-based stream by grouping functions like ,  or . Summary functions like , ,  or  are often used to define the transformation argument. For example:23Sum {getSum = 6} :> () streaming+Fold streamed items into their monoidal sumS.mconcat $ S.take 2 $ S.map (Data.Monoid.Last . Just) S.stdinLn first last"Last {getLast = Just "last"} :> () streamingThe standard way of inspecting the first item in a stream of elements, if the stream is still 'running'. The Right; case contains a Haskell pair, where the more general inspect would return a left-strict pair. There is no reason to prefer inspect since, if the Right case is exposed, the first element in the pair will have been evaluated to whnf. next :: Monad m => Stream (Of a) m r -> m (Either r (a, Stream (Of a) m r)) inspect :: Monad m => Stream (Of a) m r -> m (Either r (Of a (Stream (Of a) m r)))Interoperate with pipes producers thus: Pipes.unfoldr Stream.next :: Stream (Of a) m r -> Producer a m r Stream.unfoldr Pipes.next :: Producer a m r -> Stream (Of a) m r Similarly: IOStreams.unfoldM (fmap (either (const Nothing) Just) . next) :: Stream (Of a) IO b -> IO (InputStream a) Conduit.unfoldM (fmap (either (const Nothing) Just) . next) :: Stream (Of a) m r -> Source a m rBut see ", which is better fitted to these unfoldMs streaming"Exhaust a stream deciding whether a was an element. streaming(Remove repeated elements from a Stream.  of course accumulates a  of elements that have already been seen and should thus be used with care.4S.toList_ $ S.nubOrd $ S.take 5 S.readLn :: IO [Int]12312[1,2,3] streamingUse 7 to have a custom ordering function for your elements.  streaming3More efficient versions of above when working with  s that use .  streaming &filter p = hoist effects (partition p) streaming5Separate left and right values in distinct streams. (b; is a more powerful, functor-general, equivalent using  in place of >). So, for example, to permit unlimited user input of Int2s on condition of only two errors, we might write:S.toList $ S.print $ S.take 2 $ partitionEithers $ S.map readEither $ S.stdinLn :: IO (Of [Int] ())12qqqqqqqqqq"Prelude.read: no parse"3rrrrrrrrrr"Prelude.read: no parse" [1,2,3] :> () partitionEithers = separate . maps S.eitherToSum lefts = hoist S.effects . partitionEithers rights = S.effects . partitionEithers rights = S.concat streamingFold a 7 of numbers into their product streamingFold a 74 of numbers into their product with the return value  mapped product :: Stream (Stream (Of Int)) m r -> Stream (Of Int) m r streamingMake a stream of strings into a stream of parsed values, skipping bad cases>S.sum_ $ S.read $ S.takeWhile (/= "total") S.stdinLn :: IO Int 1000 2000 total3000 streamingRepeat an element ad inf. .S.print $ S.take 3 $ S.repeat 1111 streamingRepeat a monadic action ad inf., streaming its results.%S.toList $ S.take 2 $ repeatM getLine one two ["one","two"] streaming Repeat an element several times. streaming6Repeat an action several times, streaming its results.'S.print $ S.replicateM 2 getCurrentTime2015-08-18 00:57:36.124508 UTC2015-08-18 00:57:36.124785 UTC streamingRead an IORef (Maybe a)$ or a similar device until it reads Nothing. reread# provides convenient exit from the  io-streams library reread readIORef :: IORef (Maybe a) -> Stream (Of a) IO () reread Streams.read :: System.IO.Streams.InputStream a -> Stream (Of a) IO () streamingStrict left scan, streaming, e.g. successive partial results. The seed is yielded first, before any action of finding the next element is performed.4S.print $ S.scan (++) "" id $ each (words "a b c d")"""a""ab""abc""abcd" is fitted for use with  Control.Foldl, thus:.S.print $ L.purely S.scan L.list $ each [3..5][][3][3,4][3,4,5] streamingStrict left scan, accepting a monadic function. It can be used with FoldMs from  Control.Foldl using impurely:. Here we yield a succession of vectors each recordinglet v = L.impurely scanM L.vectorM $ each [1..4::Int] :: Stream (Of (Vector Int)) IO () S.print v[][1][1,2][1,2,3] [1,2,3,4] streamingLabel each element in a stream with a value accumulated according to a fold.3S.print $ S.scanned (*) 1 id $ S.each [100,200,300] (100,100) (200,20000) (300,6000000)=S.print $ L.purely S.scanned L.product $ S.each [100,200,300] (100,100) (200,20000) (300,6000000) streaming Like the 8 but streaming. The result type is a stream of a's, but is not accumulated; the effects of the elements of the original stream are interleaved in the resulting stream. Compare: sequence :: Monad m => [m a] -> m [a] sequence :: Monad m => Stream (Of (m a)) m r -> Stream (Of a) m rThis obeys the rule streamingFold a 7 of numbers into their sum streamingFold a 70 of numbers into their sum with the return value  mapped S.sum :: Stream (Stream (Of Int)) m r -> Stream (Of Int) m rS.sum $ each [1..10]55 :> ()2(n :> rest) <- S.sum $ S.splitAt 3 $ each [1..10]System.IO.print n6((m :> rest') <- S.sum $ S.splitAt 3 restSystem.IO.print m15 S.print rest'78910 streaming?Stream elements until one fails the condition, return the rest. streamingSplit a stream of elements wherever a given element arises. The action is like that of .S.stdoutLn $ mapped S.toList $ S.split ' ' $ each "hello world "helloworld streamingSplit a succession of layers after some number, returning a streaming or effectful pair. This function is the same as the K exported by the  Streaming module, but since this module is imported qualified, it can usurp a Prelude name. It specializes to:  splitAt :: (Monad m) => Int -> Stream (Of a) m r -> Stream (Of a) m (Stream (Of a) m r) streamingReplace each element in a stream of individual values with a functorial layer of any sort. subst = flip with and is more convenient in a sequence of compositions that transform a stream. with = flip subst for str f = concats $ subst f str subst f = maps (\(a:>r) -> r <$ f a) S.concat = concats . subst each streamingEnd a stream after n elements; the original return value is thus lost. - preserves this information. Note that, like splitAt, this function is functor-general, so that, for example, you can take not just a number of items from a stream of elements, but a number of substreams and the like.!S.toList $ S.take 3 $ each "with" "wit" :> ().S.readFile "stream.hs" (S.stdoutLn . S.take 3)import Streaming'import qualified Streaming.Prelude as S,import Streaming.Prelude (each, next, yield) streamingEnd stream when an element fails a condition; the original return value is lost. By contrast = preserves this information, and is generally more desirable. %S.takeWhile thus = void . S.span thusTo preserve the information - but thus also force the rest of the stream to be developed - write S.drained . S.span thusas dropWhile thus is S.effects . S.span thus streamingLike !, but takes a monadic predicate.  streamingConvert an effectful  Stream (Of a) into a list of asNote: Needless to say, this function does not stream properly. It is basically the same as Prelude  which, like ,  and similar operations on traversable containers is a leading cause of space leaks. streamingConvert an effectful 7' into a list alongside the return value  mapped toList :: Stream (Stream (Of a) m) m r -> Stream (Of [a]) m rLike ,  breaks streaming; unlike  it preserves the return value- and thus is frequently useful with e.g. 4S.print $ mapped S.toList $ chunksOf 3 $ each [1..9][1,2,3][4,5,6][7,8,9]?S.print $ mapped S.toList $ chunksOf 2 $ S.replicateM 4 getLinest ["s","t"]uv ["u","v"] streamingInspect the first item in a stream of elements, without a return value. uncons6 provides convenient exit into another streaming type: IOStreams.unfoldM uncons :: Stream (Of a) IO b -> IO (InputStream a) Conduit.unfoldM uncons :: Stream (Of a) m r -> Conduit.Source m a streamingBuild a Stream by unfolding steps starting from a seed. In particular note that S.unfoldr S.next = id.The seed can of course be anything, but this is one natural way to consume a pipes  . Consider::S.stdoutLn $ S.take 2 $ S.unfoldr Pipes.next Pipes.stdinLn hellohellogoodbyegoodbyeS.stdoutLn $ S.unfoldr Pipes.next (Pipes.stdinLn >-> Pipes.take 2) hellohellogoodbyegoodbyeS.effects $ S.unfoldr Pipes.next (Pipes.stdinLn >-> Pipes.take 2 >-> Pipes.stdoutLn) hellohellogoodbyegoodbyePipes.unfoldr S.next similarly unfolds a Pipes.Producer from a stream. streamingReplace each element in a stream of individual Haskell values (a Stream (Of a) m r) with an associated  functorial step. for str f = concats (with str f) with str f = for str (yields . f) with str f = maps (\(a:>r) -> r <$ f a) str with = flip subst subst = flip withwith (each [1..3]) (yield . Prelude.show) & intercalates (yield "--") & S.stdoutLn1--2--3 streamingA singleton streamstdoutLn $ yield "hello"hello&S.sum $ do {yield 1; yield 2; yield 3}6 :> ()let number = lift (putStrLn "Enter a number:") >> lift readLn >>= yield :: Stream (Of Int) IO ()&S.toList $ do {number; number; number}Enter a number:1Enter a number:2Enter a number:3 [1,2,3] :> () streamingZip two 7s streamingZip two 7's using the provided combining function streaming Zip three 7s with a combining function streaming Zip three 7 s together streamingView standard input as a Stream (Of String) m r. By contrast,  renders a Stream (Of String) m r* to standard output. The names follow  Pipes.PreludestdoutLn stdinLn hellohello worldworld^CInterrupted. stdoutLn $ S.map reverse stdinLn helloolleh worlddlrow^CInterrupted. streamingRead values from , ignoring failed parses.:set -XTypeApplications$S.sum $ S.take 2 (S.readLn @IO @Int) 10 1222 :> ()'S.toList $ S.take 2 (S.readLn @IO @Int) 101@#$%^&*\ 12 [10,12] :> () streamingRead  s from a  using Terminates on end of inputIO.withFile "/usr/share/dict/words" IO.ReadMode $ S.stdoutLn . S.take 3 . S.drop 50000 . S.fromHandle deflagratordeflate deflation streaming"hello" world"world" streamingWrite s to  using ; terminates on a broken output pipe (The name and implementation are modelled on the  Pipes.Prelude stdoutLn).S.stdoutLn $ S.take 3 $ S.each $ words "one two three four five"onetwothree streaming9Read the lines of a file, using a function of the type: '7 ( )  () ->  a/' to turn the stream into a value of type ' a'.,S.writeFile "lines.txt" $ S.take 2 S.stdinLn hello worldS.readFile "lines.txt" S.print"hello""world" streamingWrite a series of s as lines to a file.,S.writeFile "lines.txt" $ S.take 2 S.stdinLn hello world!S.readFile "lines.txt" S.stdoutLnhelloworld streamingWrite s to  using Unlike stdoutLn,  stdoutLn' does not handle a broken output pipe. Thus it can have a polymorphic return value, rather than ()4, and this kind of "connect and resume" is possible:8rest <- S.stdoutLn' $ S.show $ S.splitAt 3 (each [1..5])123 S.toList rest [4,5] :> () streamingStore the result of any suitable fold over a stream, keeping the stream for further manipulation. store f = f . copy :)S.print $ S.store S.product $ each [1..4]123424 :> ()9S.print $ S.store S.sum $ S.store S.product $ each [1..4]123410 :> (24 :> ())Here the sum (10) and the product (24) have been 'stored' for use when finally we have traversed the stream with  . Needless to say, a second pass is excluded conceptually, so the folds that you apply successively with store are performed simultaneously, and in constant memory -- as they would be if, say, you linked them together with  Control.Fold:L.impurely S.foldM (liftA3 (\a b c -> (b, c)) (L.sink Prelude.print) (L.generalize L.sum) (L.generalize L.product)) $ each [1..4]1234 (10,24) :> ()"Fusing folds after the fashion of  Control.Foldl will generally be a bit faster than the corresponding succession of uses of , but by constant factor that will be completely dwarfed when any IO is at issue.But  /  is much more powerful, as you can see by reflecting on uses like this:S.sum $ S.store (S.sum . mapped S.product . chunksOf 2) $ S.store (S.product . mapped S.sum . chunksOf 2) $ each [1..6]21 :> (44 :> (231 :> ()))It will be clear that this cannot be reproduced with any combination of lenses,  Control.Fold2 folds, or the like. (See also the discussion of .)It would conceivably be clearer to import a series of specializations of 3. It is intended to be used at types like these: storeM :: (forall s m . Monad m => Stream (Of a) m s -> m (Of b s)) -> (Monad n => Stream (Of a) n r -> Stream (Of a) n (Of b r)) storeM = store storeMIO :: (forall s m . MonadIO m => Stream (Of a) m s -> m (Of b s)) -> (MonadIO n => Stream (Of a) n r -> Stream (Of a) n (Of b r) storeMIO = storeIt is clear from these types that we are just using the general instances: instance (Functor f, Monad m) => Monad (Stream f m) instance (Functor f, MonadIO m) => MonadIO (Stream f m)We thus can't be touching the elements of the stream, or the final return value. It is the same with other constraints that  Stream (Of a). inherits from the underlying monad, like  MonadResource. Thus I can independently filter and write to one file, but nub and write to another, or interact with a database and a logfile and the like:(S.writeFile "hello2.txt" . S.nubOrd) $ store (S.writeFile "hello.txt" . S.filter (/= "world")) $ each ["hello", "world", "goodbye", "world"]:! cat hello.txthellogoodbye:! cat hello2.txthelloworldgoodbye streamingDuplicate the content of stream, so that it can be acted on twice in different ways, but without breaking streaming. Thus, with  each [1,2] I might do:S.print $ each ["one","two"]"one""two"S.stdoutLn $ each ["one","two"]onetwo)With copy, I can do these simultaneously:2S.print $ S.stdoutLn $ S.copy $ each ["one","two"]"one"one"two"two$ should be understood together with  and is subject to the rules ;S.effects . S.copy = id hoist S.effects . S.copy = idThe similar operations in  obey the same rules.Where the actions you are contemplating are each simple folds over the elements, or a selection of elements, then the coupling of the folds is often more straightforwardly effected with  , e.g.;L.purely S.fold (liftA2 (,) L.sum L.product) $ each [1..10](55,3628800) :> () rather than)S.sum $ S.product . S.copy $ each [1..10]55 :> (3628800 :> ())A  Control.Foldl fold can be altered to act on a selection of elements by using   on an appropriate lens. Some such manipulations are simpler and more  -like, using :L.purely S.fold (liftA2 (,) (L.handles (L.filtered odd) L.sum) (L.handles (L.filtered even) L.product)) $ each [1..10](25,3840) :> ()becomesS.sum $ S.filter odd $ S.product $ S.filter even $ S.copy $ each [1..10]25 :> (3840 :> ()) or using S.sum $ S.filter odd $ S.store (S.product . S.filter even) $ each [1..10]25 :> (3840 :> ())But anything that fold of a Stream (Of a) m r into e.g. an  m (Of b r) that has a constraint on m that is carried over into  Stream f m - e.g. Monad, MonadIO,  MonadResource, etc. can be used on the stream. Thus, I can fold over different groupings of the original stream:(S.toList . mapped S.toList . chunksOf 5) $ (S.toList . mapped S.toList . chunksOf 3) $ S.copy $ each [1..10][[1,2,3,4,5],[6,7,8,9,10]] :> ([[1,2,3],[4,5,6],[7,8,9],[10]] :> ())The procedure can be iterated as one pleases, as one can see from this (otherwise unadvisable!) example:(S.toList . mapped S.toList . chunksOf 4) $ (S.toList . mapped S.toList . chunksOf 3) $ S.copy $ (S.toList . mapped S.toList . chunksOf 2) $ S.copy $ each [1..12][[1,2,3,4],[5,6,7,8],[9,10,11,12]] :> ([[1,2,3],[4,5,6],[7,8,9],[10,11,12]] :> ([[1,2],[3,4],[5,6],[7,8],[9,10],[11,12]] :> ()))copy% can be considered a special case of d:  copy = d$ $ \p (a :> as) -> a :> p (a :> as) If  were an instance of , then one could write  copy = d extend  streaming An alias for copy. streamingThe type +Data.List.unzip :: [(a,b)] -> ([a],[b])might lead us to expect Streaming.unzip :: Stream (Of (a,b)) m r -> Stream (Of a) m (Stream (Of b) m r)which would not stream, since it would have to accumulate the second stream (of bs). Of course,  Data.List   doesn't stream either.This unzip does stream, though of course you can spoil this by using e.g. :>let xs = Prelude.map (\x -> (x, Prelude.show x)) [1..5 :: Int])S.toList $ S.toList $ S.unzip (S.each xs),["1","2","3","4","5"] :> ([1,2,3,4,5] :> ())Prelude.unzip xs#([1,2,3,4,5],["1","2","3","4","5"])Note the difference of order in the results. It may be of some use to think why. The first application of % was applied to a stream of integers::t S.unzip $ S.each xsS.unzip $ S.each xs :: Monad m => Stream (Of Int) (Stream (Of String) m) ()Like any fold, ) takes no notice of the monad of effects. 6toList :: Monad m => Stream (Of a) m r -> m (Of [a] r)#In the case at hand (since I am in ghci) m = Stream (Of String) IO. So when I apply , I exhaust that stream of integers, folding it into a list:!:t S.toList $ S.unzip $ S.each xsS.toList $ S.unzip $ S.each xs2 :: Monad m => Stream (Of String) m (Of [Int] ()) When I apply  to this/, I reduce everything to an ordinary action in IO#, and return a list of strings:)S.toList $ S.toList $ S.unzip (S.each xs),["1","2","3","4","5"] :> ([1,2,3,4,5] :> ()), can be considered a special case of either f or d:  unzip = f . @4 (\((a,b) :> x) -> Compose (a :> b :> x)) unzip = d* $ \p ((a,b) :> abs) -> b :> p (a :> abs)  streaming1Merge two streams of elements ordered with their  instance./The return values of both streams are returned.+S.print $ merge (each [1,3,5]) (each [2,4])12345((), ()) streamingMerge two streams, ordering them by applying the given function to each element before comparing./The return values of both streams are returned. streamingMerge two streams, ordering the elements using the given comparison function./The return values of both streams are returned. streamingThe  function takes a 7 of s and returns a 7 of all of the  values.  has the same behavior, but is more general; it works for any foldable container type. streamingThe  function is a version of  which can throw out elements. In particular, the functional argument returns something of type  b . If this is +, no element is added on to the result 7 . If it is  b, then b is included in the result 7. streaming accumulates the first n elements of a stream, update thereafter to form a sliding window of length n. It follows the behavior of the slidingWindow function in  https://hackage.haskell.org/package/conduit-combinators-1.0.4/docs/Data-Conduit-Combinators.html#v:slidingWindowconduit-combinators.-S.print $ S.slidingWindow 4 $ S.each "123456"fromList "1234"fromList "2345"fromList "3456" streaming. finds the minimum in every sliding window of n elements of a stream. If within a window there are multiple elements that are the least, it prefers the first occurrence (if you prefer to have the last occurrence, use the max version and flip your comparator). It satisfies:  n s =   ( n s) Except that it is far more efficient, especially when the window size is large: it calls  O(m) times overall where m0 is the total number of elements in the stream. streaming. finds the maximum in every sliding window of n elements of a stream. If within a window there are multiple elements that are the largest, it prefers the last occurrence (if you prefer to have the first occurrence, use the min version and flip your comparator). It satisfies:  n s =   ( n s) Except that it is far more efficient, especially when the window size is large: it calls  O(m) times overall where m0 is the total number of elements in the stream. streaming. finds the minimum in every sliding window of n elements of a stream according to the given comparison function (which should define a total ordering). See notes above about elements that are equal. It satisfies:  f n s =  ( f) ( n s) Except that it is far more efficient, especially when the window size is large: it calls the comparison function O(m) times overall where m0 is the total number of elements in the stream. streaming. finds the maximum in every sliding window of n elements of a stream according to the given comparison function (which should define a total ordering). See notes above about elements that are equal. It satisfies:  f n s =  ( f) ( n s) Except that it is far more efficient, especially when the window size is large: it calls the comparison function O(m) times overall where m0 is the total number of elements in the stream. streaming. finds the minimum in every sliding window of n elements of a stream according to the given projection function. See notes above about elements that are equal. It satisfies:  f n s =  (!" ( f)) ( n s) Except that it is far more efficient, especially when the window size is large: it calls  on the projected value O(m) times overall where m is the total number of elements in the stream, and it calls the projection function exactly m times. streaming. finds the maximum in every sliding window of n elements of a stream according to the given projection function. See notes above about elements that are equal. It satisfies:  f n s =  (!# ( f)) ( n s) Except that it is far more efficient, especially when the window size is large: it calls  on the projected value O(m) times overall where m is the total number of elements in the stream, and it calls the projection function exactly m times. streamingMap monadically over a stream, producing a new stream only containing the  values.7@Babc@Babc7 Safe-Inferred  7;<=>?@ABCDEFGHIJKLMNOPQSZ[\]^_`bcdefghijk7\[ZQOP?hj=i@BACSNg>KLMJGk]^_f`bcDdeFE$%?$%@$%ABCDBCEBCFBCGHIJHIKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~  $%$%$%$%$%$%$$$$%$$$$$$$$$$$$(streaming-0.2.3.1-JmjGcboAIpAA9PomIMwDTs StreamingData.Functor.OfStreaming.InternalStreaming.Preludeunfoldr Data.EitherpartitionEitherscopy Control.Foldlpurely Data.Maybe catMaybesrightsenumFrommapsData.SetSet Data.IntSetIntSet Data.ListsequencePipesProducerData.ByteStringControlFoldlhandlesDataListControl.ComonadComonadunzipFoldable minimumOn maximumOnbaseGHC.Base<>joinData.Functor.Compose getComposeComposeData.Functor.SumInRInLSumData.Bifunctorsecondfirstbimap BifunctorControl.Monad.IO.ClassliftIOMonadIOData.Functor.Identity runIdentityIdentity Data.FunctorvoidliftM2liftMliftA3liftA2<|> Alternative#mmorph-1.2.0-IMAEEsfHi2N4pfYW6yRFmqControl.Monad.MorphhoistMFunctorembedMMonadtransformers-0.5.6.2Control.Monad.Trans.Class MonadTransliftOf:>$fOrd2Of$fEq2Of $fShow2Of$fOrd1Of$fEq1Of $fShow1Of $fMonadOf$fApplicativeOf$fBitraversableOf$fBifoldableOf $fBifunctorOf $fFunctorOf $fMonoidOf $fSemigroupOf$fDataOf$fEqOf $fFoldableOf$fOrdOf$fReadOf$fShowOf$fTraversableOf $fGenericOf$fGeneric1TYPEOfStreamStepEffectReturndestroy streamFold streamBuildinspectunfoldmapsMmapsPost mapsMPost decomposerunmapsM_ intercalatesiterTMiterTconcatssplitsAttakeschunksOf distributerepeatsrepeatsM replicatescycleshoistUnexposed hoistExposedhoistExposedPost mapsExposed mapsMExposeddestroyExposed unexposedwrapeffectyieldszipsWith zipsWith'zips interleavesswitchseparate unseparateexpand expandPostunzipsgroupsneverdelays untilJustcutoff$fMonadErroreStream$fMonadStatesStream$fMonadReaderrStream$fMonadIOStream$fMMonadStream$fMFunctorTYPEStream$fMonadTransStream$fMonadPlusStream$fMonoidStream$fSemigroupStream$fAlternativeStream$fApplicativeStream$fMonadFailStream $fMonadStream$fFunctorStream $fOrd1Stream $fEq1Stream $fOrdStream $fEqStream$fShowShowSWrapper $fShow1Stream $fShowStreamlazilystrictlyfst'snd'mapOf_first_secondallall_anyany_break breakWhenbreakschainconcatconscycledelaydraineddrop dropWhileeacheffects wrapEffectelemelem_ enumFromThenerasefilterfilterMfold_foldfoldM_foldMfoldrTfoldrMforgroupBygroupheadhead_ intersperseiterateiterateMlastlast_length_lengthmapmapMmapM_mapped mappedPostfoldMapfoldMap_mconcatmconcat_minimumminimum_maximummaximum_nextnotElemnotElem_nubOrdnubOrdOnnubIntnubIntOn partitionproduct_productreadrepeatrepeatM replicate replicateMrereadscanscanMscannedshowsum_sumspansplitsplitAtsubsttake takeWhile takeWhileMtoList_toListuncons untilLeft untilRightwithyieldzipzipWithzipWith3zip3stdinLnreadLn fromHandletoHandleprintstdoutLnreadFile writeFile stdoutLn' distinguish sumToEither eitherToSum composeToSum sumToComposestore duplicatemergemergeOnmergeBymapMaybe slidingWindowslidingWindowMinslidingWindowMaxslidingWindowMinByslidingWindowMaxByslidingWindowMinOnslidingWindowMaxOn mapMaybeMFunctorfmapemptymempty Applicativeidghc-prim GHC.TypesIntEither Data.OldListwordsGHC.IO.Handle.FDstdinStringGHC.IO.Handle.TypesHandleGHC.IO.Handle.TexthGetLinestdout System.IOputStrLnIO GHC.ClassesOrd GHC.MaybeMaybeJustNothing Data.Foldablecompare minimumBy maximumByData.Ord comparing