Bank account example

This commit is contained in:
Tony Garnock-Jones 2016-01-16 14:32:04 -05:00
parent 6058f8ec6e
commit f1c1646163
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,22 @@
#lang prospect
;; Hello-worldish "bank account" example.
(require prospect/actor)
(struct account (balance) #:prefab)
(struct alter-balance-by (amount) #:prefab)
(actor-body->spawn-action
(lambda ()
(actor (forever #:collect [(balance 0)]
(assert (account balance))
(on (message (alter-balance-by $amount))
(+ balance amount))))
(actor (forever (on (asserted (account $balance))
(printf "Balance changed to ~a\n" balance))))
(until (asserted (observe (alter-balance-by _))))
(send! (alter-balance-by +100))
(send! (alter-balance-by -30))
))

View File

@ -0,0 +1,30 @@
#lang prospect
;; Hello-worldish "bank account" example.
(struct account (balance) #:prefab)
(struct alter-balance-by (amount) #:prefab)
(spawn (lambda (e balance)
(match e
[(message (alter-balance-by amount))
(define new-balance (+ balance amount))
(transition new-balance
(patch-seq (retract (account balance))
(assert (account new-balance))))]
[_ #f]))
0
(patch-seq (sub (alter-balance-by ?))
(assert (account 0))))
(spawn (lambda (e s)
(match e
[(patch added removed)
(for [(balance (matcher-project/set/single added (compile-projection (account (?!)))))]
(printf "Balance changed to ~a\n" balance))
#f]
[_ #f]))
(void)
(sub (account ?)))
(message (alter-balance-by +100))
(message (alter-balance-by -30))