on master help sign in
Improve Folds vs Streams documentation
c949b43Harendra Kumar
src/Streamly/Data/Fold.hs+19−8
@@ -91,26 +91,37 @@
91-- = Folds vs. Streams
91-- = Folds vs. Streams
92--
92--
93-- We can often use streams or folds to achieve the same goal. However, streams
93-- 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
94+-- are more efficient in composition of producers (e.g.
95−-- 'Streamly.Prelude.mergeBy') whereas folds allow efficient composition of
95+-- 'Streamly.Prelude.serial' or 'Streamly.Prelude.mergeBy') whereas folds are
96−-- consumers (e.g. 'serialWith', 'partition' or 'teeWith').
96+-- more efficient in composition of consumers (e.g. 'serialWith', 'partition'
97+-- or 'teeWith').
97--
98--
98-- Streams are producers, transformations on streams happen on the output side:
99-- Streams are producers, transformations on streams happen on the output side:
99--
100--
101+-- >>> :{
102+-- f =
103+-- Stream.filter odd
100−-- >>> f = Stream.sum . Stream.map (+1) . Stream.filter odd
104+-- & Stream.map (+1)
105+-- & Stream.sum
106+-- :}
101-- >>> f $ Stream.fromList [1..100]
107-- >>> f $ Stream.fromList [1..100]
102-- 2550
108-- 2550
103--
109--
104-- Folds are stream consumers with an input stream and an output value, stream
110-- Folds are stream consumers with an input stream and an output value, stream
105-- transformations on folds happen on the input side:
111-- transformations on folds happen on the input side:
106--
112--
113+-- >>> :{
114+-- f =
107−-- >>> f = Fold.filter odd $ Fold.lmap (+1) $ Fold.sum
115+-- Fold.filter odd
116+-- $ Fold.lmap (+1)
117+-- $ Fold.sum
118+-- :}
108-- >>> Stream.fold f $ Stream.fromList [1..100]
119-- >>> Stream.fold f $ Stream.fromList [1..100]
109-- 2550
120-- 2550
110--
121--
122+-- Notice the similiarity in the definition of @f@ in both cases, the only
111−-- Notice the composition by @.@ vs @$@ and the order of operations in the
123+-- difference is the composition by @&@ vs @$@ and the use @lmap@ vs @map@, the
112−-- above examples, the difference is due to output vs input side
124+-- difference is due to output vs input side transformations.
113−-- transformations.
114
125
115module Streamly.Data.Fold
126module Streamly.Data.Fold
116 (
127 (