Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add weaving utils #222

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add weaving utils #222

wants to merge 2 commits into from

Conversation

alexfmpe
Copy link
Contributor

I stumbled into weaveMax during some experiments with Free and one thing lead to another.
If these are considered general/useful enough, I'd be up for implementing them for the other free encodings and adding comments.

Usage examples:

game :: Free IO (Maybe String)
game = Free $ getLine >>= \case
  "abracadabra" -> pure $ Pure $ Just "some reward"
  _ -> game <$ putStrLn "[Sorry, try again]"
> retract $ before (putStrLn "[Round start]") game
[Round start]
foo
[Sorry, try again]
[Round start]
bar
[Sorry, try again]
[Round start]
abracadabra
Just "some reward"

> retract $ after (putStrLn "[Round end]") game
foo
[Sorry, try again]
[Round end]
bar
[Sorry, try again]
[Round end]
abracadabra
[Round end]
Just "some reward"

> retract $ weaveMin (forever $ liftF $ putStrLn "[Round start]") game
[Round start]
foo
[Sorry, try again]
[Round start]
bar
[Sorry, try again]
[Round start]
abracadabra

> retract $ weaveMax game (for_ [0..1] $ \i -> liftF (putStrLn ("[Hint #" <> show i <> "]")))
foo
[Sorry, try again]
[Hint #0]
bar
[Sorry, try again]
[Hint #1]
baz
[Sorry, try again]
abracadabra
(Just "some reward",())

@alexfmpe alexfmpe marked this pull request as ready for review March 16, 2023 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant