@@ -89,6 +89,7 @@ module Streamly.Internal.Data.Stream.StreamD.Type
89 , concatMapM
89 , concatMapM
90 , concatEffect
90 , concatEffect
92+ -- * Expanding trees top down
92 , unfoldIterateDfs
93 , unfoldIterateDfs
93 , unfoldIterateBfs
94 , unfoldIterateBfs
94 , unfoldIterateBfsRev
95 , unfoldIterateBfsRev
@@ -105,6 +106,10 @@ module Streamly.Internal.Data.Stream.StreamD.Type
105 , chunksOf
106 , chunksOf
107 , refoldMany
108 , refoldMany
110+ -- * Folding trees bottom up
@@ -894,6 +899,10 @@ instance Monad m => Monad (Stream m) where
894 (>>) = (*>)
899 (>>) = (*>)
902+------------------------------------------------------------------------------
903+-- Traversing a tree top down
904+------------------------------------------------------------------------------
897-- | Generate a stream from an initial state, scan and concat the stream,
906-- | Generate a stream from an initial state, scan and concat the stream,
898-- generate a stream again from the final state of the previous scan and repeat
907-- generate a stream again from the final state of the previous scan and repeat
899-- the process.
908-- the process.
@@ -1130,6 +1139,61 @@ unfoldIterateBfs (Unfold istep inject) (Stream ostep ost) =
1130 (y:ys) -> return $ Skip (IterateUnfoldBFSInner y ys [])
1139 (y:ys) -> return $ Skip (IterateUnfoldBFSInner y ys [])
1131 [] -> return Stop
1140 [] -> return Stop
1142+------------------------------------------------------------------------------
1143+-- Folding a tree bottom up
1144+------------------------------------------------------------------------------
1146+-- | Binary BFS style reduce, folds a level entirely, before starting to fold
1147+-- the next level. The last elements of a previously folded level are folded
1149+{-# INLINE_NORMAL reduceIterateBfs #-}
1150+reduceIterateBfs :: Monad m =>
1151+ (a -> a -> m a) -> Stream m a -> m (Maybe a)
1152+reduceIterateBfs f (Stream step state) = go SPEC state [] Nothing
1156+ go _ st xs Nothing = do
1157+ r <- step defState st
1159+ Yield x1 s -> go SPEC s xs (Just x1)
1160+ Skip s -> go SPEC s xs Nothing
1163+ [] -> return Nothing
1164+ _ -> goBuf SPEC xs []
1165+ go _ st xs (Just x1) = do
1166+ r2 <- step defState st
1170+ go SPEC s (x:xs) Nothing
1171+ Skip s -> go SPEC s xs (Just x1)
1174+ [] -> return (Just x1)
1175+ _ -> goBuf SPEC (x1:xs) []
1177+ goBuf _ [] ys = goBuf SPEC ys []
1178+ goBuf _ [x1] ys = do
1180+ [] -> return (Just x1)
1184+ goBuf _ (x1:x2:xs) ys = do
1186+ goBuf SPEC xs (y:ys)
1188+-- | N-Ary BFS style iterative fold, if the input stream finished before the
1189+-- fold then it returns Left otherwise Right. If the fold returns Left we
1194+ Fold m a (Either a a) -> Stream m a -> m (Maybe a)
1195+foldIterateBfs = undefined
1133------------------------------------------------------------------------------
1197------------------------------------------------------------------------------
1134-- Grouping/Splitting
1198-- Grouping/Splitting
1135------------------------------------------------------------------------------
1199------------------------------------------------------------------------------