on master help sign in
update fold documentation
1889b4eHarendra Kumar
src/Streamly/Internal/Data/Fold.hs+1−1
@@ -744,7 +744,7 @@ head = _Fold1 const
744--
744--
745-- @
745-- @
746-- find p = fmap getFirst $
746-- find p = fmap getFirst $
747−-- foldMap (\x -> First (if p x then Just x else Nothing))
747+-- FL.foldMap (\x -> First (if p x then Just x else Nothing))
748-- @
748-- @
749--
749--
750-- @since 0.7.0
750-- @since 0.7.0
src/Streamly/Internal/Prelude.hs+2−2
@@ -3196,9 +3196,9 @@ splitParseTill = undefined
3196--
3196--
3197-- | Iterate a parser generating function on a stream. The initial value @b@ is
3197-- | Iterate a parser generating function on a stream. The initial value @b@ is
3198-- used to generate the first parser, the parser is applied on the stream and
3198-- used to generate the first parser, the parser is applied on the stream and
3199−-- the result is used generate the next parser and so on.
3199+-- the result is used to generate the next parser and so on.
3200--
3200--
3201−-- >>> S.toList $ S.concatParse (\b -> PR.take 2 (FL.mconcatTo b)) 0 $ S.fromList [1..10]
3201+-- >>> S.toList $ S.map getSum $ S.concatParse (\b -> PR.take 2 (FL.mconcatTo b)) 0 $ S.map Sum $ S.fromList [1..10]
3202-- > [3,10,21,36,55,55]
3202-- > [3,10,21,36,55,55]
3203--
3203--
3204-- This is the streaming equivalent of monad like sequenced application of
3204-- This is the streaming equivalent of monad like sequenced application of
src/Streamly/Prelude.hs+25−9
@@ -120,15 +120,31 @@ module Streamly.Prelude
120 -- ** Folding
120 -- ** Folding
121-- | In imperative terms a fold can be considered as a loop over the stream
121-- | In imperative terms a fold can be considered as a loop over the stream
122-- that reduces the stream to a single value.
122-- that reduces the stream to a single value.
123−-- Left and right folds use a fold function @f@ and an identity element @z@
123+-- Left and right folds both use a fold function @f@ and an identity element
124−-- (@zero@) to recursively deconstruct a structure and then combine and reduce
124+-- @z@ (@zero@) to deconstruct a recursive data structure and reconstruct a
125−-- the values or transform and reconstruct a new container.
125+-- new data structure. The new structure may be a recursive construction (a
126+-- container) or a non-recursive single value reduction of the original
127+-- structure.
126−--
128+--
127−-- In general, a right fold is suitable for transforming and reconstructing a
129+-- Both right and left folds are mathematical duals of each other, they are
130+-- functionally equivalent. Operationally, a left fold on a left associated
131+-- structure behaves exactly in the same way as a right fold on a right
132+-- associated structure. Similarly, a left fold on a right associated structure
133+-- behaves in the same way as a right fold on a left associated structure.
128−-- right associated structure (e.g. cons lists and streamly streams) and a left
134+-- However, the behavior of a right fold on a right associated structure is
129−-- fold is suitable for reducing a right associated structure. The behavior of
135+-- operationally different (even though functionally equivalent) than a left
136+-- fold on the same structure.
137+--
138+-- On right associated structures like Haskell @cons@ lists or Streamly
139+-- streams, a lazy right fold is naturally suitable for lazy recursive
140+-- reconstruction of a new structure, while a strict left fold is naturally
141+-- suitable for efficient reduction. In right folds control is in the hand of
142+-- the @puller@ whereas in left folds the control is in the hand of the
143+-- @pusher@.
144+--
130−-- right and left folds are described in detail in the individual fold's
145+-- The behavior of right and left folds are described in detail in the
131−-- documentation. To illustrate the two folds for cons lists:
146+-- individual fold's documentation. To illustrate the two folds for right
147+-- associated @cons@ lists:
132--
148--
133-- > foldr :: (a -> b -> b) -> b -> [a] -> b
149-- > foldr :: (a -> b -> b) -> b -> [a] -> b
134-- > foldr f z [] = z
150-- > foldr f z [] = z