Add Receive synonym, receive and feedEach

This commit is contained in:
Tom Ellis 2024-09-30 12:25:23 +01:00
parent f2923fc0f4
commit 9173563b18

View File

@ -138,7 +138,7 @@ connectCoroutines m1 m2 = unsafeProvideIO $ \io -> do
race (useImplWithin t1) (useImplWithin t2) io
receiveStream ::
(forall e. Coroutine () a e -> Eff (e :& es) r) ->
(forall e. Receive a e -> Eff (e :& es) r) ->
(forall e. Stream a e -> Eff (e :& es) r) ->
Eff es r
receiveStream r s = connectCoroutines r (\() -> s)
@ -271,6 +271,8 @@ newtype Coroutine a b (e :: Effects) = MkCoroutine (a -> Eff e b)
-- @a@ and then expects values of type @()@.
type Stream a = Coroutine a ()
type Receive a = Coroutine () a
-- | You can define a @Handle@ instance for your compound handles. As
-- an example, an "application" handle with a dynamic effect for
-- database queries, a concrete effect for application state and a
@ -722,6 +724,22 @@ enumerateFrom n ss st =
yield st (ii, s)
put i (ii + 1)
-- | A version of 'forEach' specialized to @Receive@. Every time the
-- @Receive@ attempts to @receive@ a @b@, feed it the one created by
-- the handler.
feedEach ::
( forall e.
Receive b e ->
Eff (e :& es) r
) ->
-- | Value to send to each @receive@ in the body.
Eff es b ->
Eff es r
feedEach k e = forEach k (\() -> e)
receive :: (e :> es) => Receive a e -> Eff es a
receive r = yieldCoroutine r ()
type EarlyReturn = Exception
-- | Run an 'Eff' action with the ability to return early to this