Added counter (become) example.
authorTim Vaughan <tgvaughan@gmail.com>
Sun, 7 Jul 2019 21:42:35 +0000 (23:42 +0200)
committerTim Vaughan <tgvaughan@gmail.com>
Sun, 7 Jul 2019 21:42:35 +0000 (23:42 +0200)
actors.scm
test_counter.scm [new file with mode: 0644]

index fa6f1bb..45af822 100644 (file)
     (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 (file)
index 0000000..55a0812
--- /dev/null
@@ -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)