Rearrange bank-account examples.
This commit is contained in:
parent
1d28908600
commit
5e6d72b991
|
@ -4,19 +4,19 @@
|
||||||
(require prospect/actor)
|
(require prospect/actor)
|
||||||
|
|
||||||
(struct account (balance) #:prefab)
|
(struct account (balance) #:prefab)
|
||||||
(struct alter-balance-by (amount) #:prefab)
|
(struct deposit (amount) #:prefab)
|
||||||
|
|
||||||
(actor-body->spawn-action
|
(actor-body->spawn-action
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(actor (forever #:collect [(balance 0)]
|
(actor (forever #:collect [(balance 0)]
|
||||||
(assert (account balance))
|
(assert (account balance))
|
||||||
(on (message (alter-balance-by $amount))
|
(on (message (deposit $amount))
|
||||||
(+ balance amount))))
|
(+ balance amount))))
|
||||||
|
|
||||||
(actor (forever (on (asserted (account $balance))
|
(actor (forever (on (asserted (account $balance))
|
||||||
(printf "Balance changed to ~a\n" balance))))
|
(printf "Balance changed to ~a\n" balance))))
|
||||||
|
|
||||||
(until (asserted (observe (alter-balance-by _))))
|
(until (asserted (observe (deposit _))))
|
||||||
(send! (alter-balance-by +100))
|
(send! (deposit +100))
|
||||||
(send! (alter-balance-by -30))
|
(send! (deposit -30))
|
||||||
))
|
))
|
||||||
|
|
|
@ -2,34 +2,27 @@
|
||||||
;; Hello-worldish "bank account" example.
|
;; Hello-worldish "bank account" example.
|
||||||
|
|
||||||
(struct account (balance) #:prefab)
|
(struct account (balance) #:prefab)
|
||||||
(struct alter-balance-by (amount) #:prefab)
|
(struct deposit (amount) #:prefab)
|
||||||
|
|
||||||
(spawn (lambda (e balance)
|
(define (manager e balance)
|
||||||
(match e
|
(match e
|
||||||
[(message (alter-balance-by amount))
|
[(message (deposit amount))
|
||||||
(define new-balance (+ balance amount))
|
(transition (+ balance amount) (patch-seq (retract (account balance))
|
||||||
(transition new-balance
|
(assert (account (+ balance amount)))))]
|
||||||
(patch-seq (retract (account balance))
|
[_ #f]))
|
||||||
(assert (account new-balance))))]
|
|
||||||
[_ #f]))
|
|
||||||
0
|
|
||||||
(patch-seq (assert (observe (alter-balance-by ?)))
|
|
||||||
(assert (account 0))))
|
|
||||||
|
|
||||||
(spawn (lambda (e s)
|
(define (observer e _)
|
||||||
(match e
|
(when (patch? e)
|
||||||
[(patch added removed)
|
(for [(balance (project-assertions (patch-added e) (account (?!))))]
|
||||||
(for [(balance (project-assertions added (account (?!))))]
|
(printf "Balance changed to ~a\n" balance)))
|
||||||
(printf "Balance changed to ~a\n" balance))
|
#f)
|
||||||
#f]
|
|
||||||
[_ #f]))
|
|
||||||
(void)
|
|
||||||
(assert (observe (account ?))))
|
|
||||||
|
|
||||||
(spawn (lambda (e s)
|
(define (updater e _)
|
||||||
(if (and (patch? e) (matcher-non-empty? (patch-added e)))
|
(if (and (patch? e) (matcher-non-empty? (patch-added e)))
|
||||||
(quit (list (message (alter-balance-by +100))
|
(quit (list (message (deposit +100))
|
||||||
(message (alter-balance-by -30))))
|
(message (deposit -30))))
|
||||||
#f))
|
#f))
|
||||||
(void)
|
|
||||||
(assert (observe (observe (alter-balance-by ?)))))
|
(spawn manager 0 (patch-seq (assert (observe (deposit ?))) (assert (account 0))))
|
||||||
|
(spawn observer (void) (assert (observe (account ?))))
|
||||||
|
(spawn updater (void) (assert (observe (observe (deposit ?)))))
|
||||||
|
|
Loading…
Reference in New Issue