New examples

This commit is contained in:
Tony Garnock-Jones 2017-09-28 16:26:01 +01:00
parent e41290c509
commit 51ab2921c2
2 changed files with 46 additions and 6 deletions

View File

@ -0,0 +1,39 @@
#lang syndicate
(assertion-struct cell (id value))
(message-struct create-cell (id value))
(message-struct update-cell (id value))
(message-struct delete-cell (id))
(define (spawn-cell initial-value)
(define id (gensym 'cell))
(send! (create-cell id initial-value))
id)
(spawn #:name 'cell-factory
(on (message (create-cell $id $initial-value))
(spawn #:name (list 'cell id)
(field [value initial-value])
(assert (cell id (value)))
(on (message (update-cell id $new-value)) (value new-value))
(stop-when (message (delete-cell id))))))
(define (spawn-cell-monitor id)
(spawn #:name (list 'cell-monitor id)
(on (asserted (cell id $value))
(printf "Cell ~a updated to: ~a\n" id value))
(on (retracted (cell id _))
(printf "Cell ~a deleted\n" id))))
(define (read-cell id)
(flush!) ;; this is important! else previous writes remain buffered after the first read
(react/suspend (k)
(stop-when (asserted (cell id $value)) (k value))))
(spawn* #:name 'main-actor
(define id (spawn-cell 123))
(spawn-cell-monitor id)
(send! (update-cell id (+ (read-cell id) 1)))
(send! (update-cell id (+ (read-cell id) 1)))
(send! (update-cell id (+ (read-cell id) 1)))
(send! (delete-cell id)))

View File

@ -2,10 +2,11 @@
(assertion-struct greeting (text))
(spawn (assert (greeting "Hello from an outer dataspace actor!")))
(spawn (on (asserted (greeting $t))
(printf "Outer dataspace: ~a\n" t)))
(spawn #:name "A" (assert (greeting "Hi from outer space!")))
(spawn #:name "B" (on (asserted (greeting $t))
(printf "Outer dataspace: ~a\n" t)))
(dataspace (spawn (assert (outbound (greeting "Hello from an inner dataspace actor!"))))
(spawn (on (asserted (inbound (greeting $t)))
(printf "Inner dataspace: ~a\n" t))))
(dataspace #:name "C"
(spawn #:name "D" (assert (outbound (greeting "Hi from inner!"))))
(spawn #:name "E" (on (asserted (inbound (greeting $t)))
(printf "Inner dataspace: ~a\n" t))))