Skip to content

Commit

Permalink
Add mapRec and refactor receiveUsage
Browse files Browse the repository at this point in the history
  • Loading branch information
tomjaguarpaw committed Aug 23, 2024
1 parent 11b2763 commit 11df9ab
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions bluefin-internal/src/Bluefin/Internal/Examples.hs
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,32 @@ takeRec n done k rec = evalState n $ \s -> forEach k $ \() -> do
modify s (subtract 1)
receive rec

mapRec ::
(e :> es) =>
(a -> b) ->
(forall e1. Receive b e1 -> Eff (e1 :& es) ()) ->
Receive a e ->
Eff es ()
mapRec f k rec = forEach k $ \() -> do
r <- receive rec
pure (f r)

receiveUsage ::
(e :> es, e3 :> es) =>
(e :> es, e3 :> es, e2 :> es) =>
IOE e3 ->
Jump e ->
Receive Int e2 ->
Eff es ()
receiveUsage io done = do
receiveList [1 :: Int .. 5] io done $ takeRec 2 done $ \rec -> do
mapRec (* 11) $ takeRec 2 done $ \rec -> do
for_ [1 :: Int .. 3] $ \_ -> do
i <- receive rec
effIO io (print i)

receiveExample :: IO ()
receiveExample = runEff $ \io -> do
withJump $ \done -> do
receiveUsage io done
receiveList [1 :: Int .. 5] io done $ receiveUsage io done

-- Count the number of (strictly) positives and (strictly) negatives
-- in a list, unless we see a zero, in which case we bail with an
Expand Down

0 comments on commit 11df9ab

Please sign in to comment.