on master help sign in
Add some todo notes/combinator definitions
commitd50214ab30709abe34aacf53ee460168d5d0c4af authorHarendra Kumar <harendra.kumar@gmail.com> dateWed 29 May 2019, 09:58 (7 years ago) parent523f606
2 files changed+60
src/Streamly/Streams/StreamK.hs+17 src/Streamly/Streams/StreamK/Type.hs+43
src/Streamly/Streams/StreamK.hs+17
@@ -229,6 +229,23 @@ unfoldrM step = go
229229 Just (a, b) -> yld a (go b)
230230 Nothing -> stp
231231
232+{-
233+-- Generalization of concurrent streams/SVar via unfoldr.
234+--
235+-- Unfold a value into monadic actions and then run the resulting monadic
236+-- actions to generate a stream. Since the step of generating the monadic
237+-- action and running them are decoupled we can run the monadic actions
238+-- cooncurrently. For example, the seed could be a list of monadic actions or a
239+-- pure stream of monadic actions.
240+--
241+-- We can have different flavors of this depending on the stream type t. The
242+-- concurrent version could be async or ahead etc. Depending on how we queue
243+-- back the feedback portion b, it could be DFS or BFS style.
244+--
245+unfoldrA :: (IsStream t, MonadAsync m) => (b -> Maybe (m a, b)) -> b -> t m a
246+unfoldrA = undefined
247+-}
248+
232249-------------------------------------------------------------------------------
233250-- Special generation
234251-------------------------------------------------------------------------------
src/Streamly/Streams/StreamK/Type.hs+43
@@ -939,3 +939,46 @@ instance Monad m => Monad (Stream m) where
939939 return = pure
940940 {-# INLINE (>>=) #-}
941941 (>>=) = flip concatMap
942+
943+{-
944+-- ConcatMap recursively on itself using a merge strategy.
945+concatLoopBy :: IsStream t
946+ => (forall c. t m c -> t m c -> t m c)
947+ -> (a -> t m a) -> t m a
948+concatLoopBy = undefined
949+
950+-- This is mfix. Put another way, concatMap recursively on the output of a
951+-- stream. Compare with iterate.
952+concatLoop :: IsStream t => (a -> t m a) -> t m a
953+concatLoop = concatLoopBy serial
954+
955+instance MonadFix (Stream m) where
956+ mfix = concatLoop
957+
958+-- The SVar implementation is something similar to concatFeedBack, so maybe
959+-- there is an opportunity to share the implementation here. In an SVar we run
960+-- a part of an action (a stream), it yields an output and the rest of the
961+-- stream, we yield the output and queue back the rest of the stream for
962+-- further evaluation. Also see unfoldrA.
963+--
964+-- There could be multiple variants of this combinator, for example use a
965+-- specific way of concating i.e. concatLoopBy. Which also includes combinators
966+-- with different stop behaviors. For example if the Left values are errors we
967+-- can stop the whole composition on errors or on specific errors.
968+--
969+-- We can also flip the serial/ahead append behavior e.g. instead of processing
970+-- the Left output after the original stream we can reverse the order.
971+--
972+-- | Concat map on the 'Left' output of a stream and merge it back into the
973+-- stream. The right output is yielded in the output stream.
974+concatFeedBack :: IsStream t
975+ => (b -> t m (Either a b)) -> t m (Either a b) -> t m a
976+concatFeedBack = undefined
977+
978+-- Compare this with unfoldr. Start with a seed stream and generate a stream
979+-- with a value and new seeds. The new seeds are fed back to generate a seed
980+-- stream and so on. This is a stream level unfoldr.
981+concatUnfoldr :: IsStream t
982+ => (b -> t m (Maybe (a, b))) -> t m (Maybe (a, b)) -> t m a
983+concatUnfoldr = undefined
984+-}