Skip to content

Add weaving utils#222

Open
alexfmpe wants to merge 2 commits into
ekmett:masterfrom
alexfmpe:weaving
Open

Add weaving utils#222
alexfmpe wants to merge 2 commits into
ekmett:masterfrom
alexfmpe:weaving

Conversation

@alexfmpe

Copy link
Copy Markdown
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