Add withYieldToList

This commit is contained in:
Tom Ellis 2024-04-27 17:22:04 +01:00
parent aa154c4bb0
commit 50bf7c9f3f
3 changed files with 26 additions and 0 deletions

View File

@ -777,6 +777,24 @@ yieldToList f = do
(as, r) <- yieldToReverseList f
pure (reverse as, r)
-- |
-- @
-- >>> runPureEff $ withYieldToList $ \y -> do
-- yield y 1
-- yield y 2
-- yield y 100
-- pure length
-- 3
-- @
withYieldToList ::
-- | Stream computation
(forall e. Stream a e -> Eff (e :& es) ([a] -> r)) ->
-- | Result
Eff es r
withYieldToList f = do
(l, g) <- yieldToList f
pure (g l)
-- | This is more efficient than 'yieldToList' because it gathers the
-- elements into a stack in reverse order. @yieldToList@ then reverses
-- that stack.

View File

@ -61,6 +61,13 @@ yieldExample = runPureEff $ yieldToList $ \y -> do
yield y 2
yield y 100
withYieldToListExample :: Int
withYieldToListExample = runPureEff $ withYieldToList $ \y -> do
yield y 1
yield y 2
yield y 100
pure length
forEachExample :: ([Int], ())
forEachExample = runPureEff $ yieldToList $ \y -> do
forEach (inFoldable [0 .. 4]) $ \i -> do

View File

@ -12,6 +12,7 @@ module Bluefin.Stream
forEach,
yieldToList,
yieldToReverseList,
withYieldToList,
enumerate,
enumerateFrom,
mapMaybe,