From: Tim Vaughan Date: Sun, 7 Jul 2019 16:20:42 +0000 (+0200) Subject: Factorial example works. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=db7af542227afb36f567309eaca6a04ff4bb233b;p=actors.git Factorial example works. --- diff --git a/actors.scm b/actors.scm index b8e52a8..9a1b630 100644 --- a/actors.scm +++ b/actors.scm @@ -13,7 +13,7 @@ (define next-actor-address 1) (define (make-actor behaviour) - (make-actor-with-id next-actor-address behaviour) + (make-actor-with-address next-actor-address behaviour) (let ((address next-actor-address)) (set! next-actor-address (+ next-actor-address 1)) address)) @@ -23,9 +23,14 @@ ;;; (define (dispatch-message address message) - (let ((behaviour (hash-table-ref actor-table address))) - (unless (null? behaviour) - ((hash-table-ref actor-table address)))) message) + (print "Dispatching message " message " to " address) + (let* ((behaviour (hash-table-ref actor-table address)) + (value (behaviour address message))) + (match value + ('done (hash-table-delete! actor-table address)) + ('sleep 'do-nothing) + (else + hash-table-set! actor-table address value)))) ;;; ;;; FIFO queue implementation @@ -90,16 +95,28 @@ (fifo-pop message-queue))) (define (send-message actor message) + (print "Queued message " message " to " actor) (fifo-push message-queue (cons actor message))) (define (run) (let ((addressed-msg (next-addressed-msg))) - (if (null? msg) + (if (null? addressed-msg) 'done - (begin - (apply dispatch-message addressed-msg) + (let ((address (car addressed-msg)) + (message (cdr addressed-msg))) + (dispatch-message address message) (run))))) ;;; -;;; Send +;;; Testing ;;; + +(define factorial + (make-actor + (lambda (self message) + (match message + ((n) (send-message self (list n 1)) 'sleep) + ((0 acc) (print acc) 'done) + ((n acc) (send-message self (list (- n 1) (* acc n))) 'sleep))))) + +;(send-message factorial '(5))