on master help sign in
Improve Folds vs Streams documentation
commitc949b4388521b1b06e8669954442cb04cc705cb1 authorHarendra Kumar <harendra@composewell.com> dateWed 28 Jul 2021, 17:32 (5 years ago) parenta43818d
1 file changed+19−8
src/Streamly/Data/Fold.hs+19−8
src/Streamly/Data/Fold.hs+19−8
@@ -91,26 +91,37 @@
9191-- = Folds vs. Streams
9292--
9393-- We can often use streams or folds to achieve the same goal. However, streams
94−-- allow efficient composition of producers (e.g. 'Streamly.Prelude.serial' or
95−-- 'Streamly.Prelude.mergeBy') whereas folds allow efficient composition of
96−-- consumers (e.g. 'serialWith', 'partition' or 'teeWith').
94+-- are more efficient in composition of producers (e.g.
95+-- 'Streamly.Prelude.serial' or 'Streamly.Prelude.mergeBy') whereas folds are
96+-- more efficient in composition of consumers (e.g. 'serialWith', 'partition'
97+-- or 'teeWith').
9798--
9899-- Streams are producers, transformations on streams happen on the output side:
99100--
100−-- >>> f = Stream.sum . Stream.map (+1) . Stream.filter odd
101+-- >>> :{
102+-- f =
103+-- Stream.filter odd
104+-- & Stream.map (+1)
105+-- & Stream.sum
106+-- :}
101107-- >>> f $ Stream.fromList [1..100]
102108-- 2550
103109--
104110-- Folds are stream consumers with an input stream and an output value, stream
105111-- transformations on folds happen on the input side:
106112--
107−-- >>> f = Fold.filter odd $ Fold.lmap (+1) $ Fold.sum
113+-- >>> :{
114+-- f =
115+-- Fold.filter odd
116+-- $ Fold.lmap (+1)
117+-- $ Fold.sum
118+-- :}
108119-- >>> Stream.fold f $ Stream.fromList [1..100]
109120-- 2550
110121--
111−-- Notice the composition by @.@ vs @$@ and the order of operations in the
112−-- above examples, the difference is due to output vs input side
113−-- transformations.
122+-- Notice the similiarity in the definition of @f@ in both cases, the only
123+-- difference is the composition by @&@ vs @$@ and the use @lmap@ vs @map@, the
124+-- difference is due to output vs input side transformations.
114125
115126module Streamly.Data.Fold
116127 (