box-and-client example

This commit is contained in:
Tony Garnock-Jones 2015-11-02 21:40:12 -05:00
parent 212900bc1a
commit 7b72d40a70
2 changed files with 45 additions and 0 deletions

View File

@ -42,3 +42,22 @@ and `I` needs to be clarified, among many other things.
(define-syntax-rule (forever O ...)
(until (rising-edge #f) O ...))
## Examples
```racket
#lang prospect/actor
;; Simple mutable box and count-to-infinity box client.
(struct set-box (new-value) #:transparent)
(struct box-state (value) #:transparent)
(actor (forever #:collect [(value 0)]
(assert (box-state value))
(on (message (set-box $new-value))
new-value)))
(actor (forever
(on (asserted (box-state $value))
(send! (set-box (+ value 1))))))
```

View File

@ -0,0 +1,26 @@
#lang prospect
;; Simple mutable box and count-to-infinity box client.
(struct set-box (new-value) #:transparent)
(struct box-state (value) #:transparent)
(spawn (lambda (e current-value)
(match e
[(message (set-box new-value))
(transition new-value (patch-seq (retract (box-state current-value))
(assert (box-state new-value))))]
[_ #f]))
0
(sub (set-box ?))
(assert (box-state 0)))
(spawn (lambda (e s)
(match e
[(patch added removed)
(transition s (for/list [(v (matcher-project/set/single
added
(compile-projection (box-state (?!)))))]
(message (set-box (+ v 1)))))]
[_ #f]))
(void)
(sub (box-state ?)))