Bank account example
This commit is contained in:
parent
6058f8ec6e
commit
f1c1646163
|
@ -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))
|
||||
))
|
|
@ -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))
|
Loading…
Reference in New Issue