Skip to content

Commit

Permalink
Sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Bowen Fu committed Aug 2, 2022
1 parent 88e5450 commit 659d308
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,33 @@ auto io_ = do_(
io_.run();
```
And we can also compose two STMs with `orElse`
```c++
// (limitedWithdraw2 acc1 acc2 amt) withdraws amt from acc1,
// if acc1 has enough money, otherwise from acc2.
// If neither has enough, it retries.
constexpr auto limitedWithdraw2 = toFunc<> | [](Account acc1, Account acc2, Integer amt)
{
return orElse | (limitedWithdrawSTM | acc1 | amt) | (limitedWithdrawSTM | acc2 | amt);
};
Id<Account> acc1, acc2;
auto io_ = do_(
acc1 <= (atomically | (newTVar | Integer{100})),
acc2 <= (atomically | (newTVar | Integer{100})),
showAcc | "Left pocket" | acc1,
showAcc | "Right pocket" | acc2,
forkIO | (delayDeposit | acc2 | 1),
print | "Withdrawing $101 from either pocket...",
atomically | (limitedWithdraw2 | acc1 | acc2 | Integer{101}),
print | "Successful withdrawal!",
showAcc | "Left pocket" | acc1,
showAcc | "Right pocket" | acc2
);
io_.run();
```

## Haskell vs Hspp (Incomplete list)

| Haskell | Hspp |
Expand Down

0 comments on commit 659d308

Please sign in to comment.