From: Tim Vaughan Date: Sun, 7 Jul 2019 21:42:35 +0000 (+0200) Subject: Added counter (become) example. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=10a0ca76197de20a21ca0a8934566090edb5dbe4;p=actors.git Added counter (become) example. --- diff --git a/actors.scm b/actors.scm index fa6f1bb..45af822 100644 --- a/actors.scm +++ b/actors.scm @@ -31,11 +31,13 @@ (if (null? behaviour) (print "Warning: discarded message " message " to unknown actor " address) (let ((value (apply behaviour (cons address message)))) - (match value - ('done (hash-table-delete! actor-table address)) - ('sleep 'do-nothing) + (case value + ((done) (hash-table-delete! actor-table address)) + ((sleep) 'do-nothing) (else - hash-table-set! actor-table address value)))))) + (if trace-enabled + (print "Updating behaviour of " address)) + (hash-table-set! actor-table address value))))))) ;;; ;;; FIFO queue implementation diff --git a/test_counter.scm b/test_counter.scm new file mode 100644 index 0000000..55a0812 --- /dev/null +++ b/test_counter.scm @@ -0,0 +1,22 @@ +(load "actors.scm") + +(define trace-enabled #t) + +(define ((make-counter-behaviour value) self customer . args) + (match args + (('get) (send-message customer value) 'sleep) + (('inc delta) (make-counter-behaviour (+ value delta))))) + +(define println + (make-actor-with-address 'println + (lambda (self . message) + (apply print message) + 'sleep))) + +(define counter0 (make-actor (make-counter-behaviour 0))) + +(send-and-run counter0 println 'get) +(send-message counter0 println 'inc 3) +(send-message counter0 println 'get) +(send-message counter0 println 'inc 2) +(send-and-run counter0 println 'get)