From 463910809278581e2f9ef916941170585c343b31 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Sun, 13 Apr 2025 15:13:54 +0200 Subject: [PATCH] core/actor: example actor, with function mapping: calculator --- core/actor.lisp | 25 +++++++++++++++++-------- test/test-core.lisp | 8 ++++---- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/core/actor.lisp b/core/actor.lisp index 63d2f4a..bfc5ddb 100644 --- a/core/actor.lisp +++ b/core/actor.lisp @@ -4,7 +4,8 @@ (:use :common-lisp) (:local-nicknames (:util :scopes/util)) (:export #:actor #:become #:create #:send - #:echo #:inc #:lgi)) + #:echo #:inc #:lgi + #:calculator #:plus #:minus #:show)) (in-package :scopes/core/actor) @@ -14,7 +15,7 @@ ((content :reader content :initarg :content :initform nil) (customer :reader customer :initarg :customer :initform nil))) -(defun message (content customer) +(defun message (content &optional customer) (make-instance 'message :content content :customer customer)) (defclass actor () @@ -46,9 +47,17 @@ (defun echo (ac msg) (send (customer msg) msg)) -(defun inc (&optional (val 0)) - #'(lambda (ac msg) - (let ((c (content msg))) - (if (eq c :show) - (send (create #'lgi) val) - (become ac (inc (+ c val))))))) +;;;; example behavior: calculator + +(defun calculator (&optional (val 0)) + #'(lambda (ac msg) + (destructuring-bind (fn &optional param) (content msg) + (funcall fn ac val param)))) + +(defun plus (ac val param) + (become ac (calculator (+ val param)))) +(defun minus (ac val param) + (become ac (calculator (- val param)))) +(defun show (ac val param) + (send (create #'lgi) val)) + diff --git a/test/test-core.lisp b/test/test-core.lisp index 04fde98..5a42d94 100644 --- a/test/test-core.lisp +++ b/test/test-core.lisp @@ -128,10 +128,10 @@ (deftest test-actor () (let (a1 a2 a3) - (setf a1 (actor:create (actor:inc))) - (actor:send a1 2) - (actor:send a1 3) - (actor:send a1 :show) + (setf a1 (actor:create (actor:calculator))) + (actor:send a1 '(actor:plus 2)) + (actor:send a1 '(actor:minus 3)) + (actor:send a1 '(actor:show)) )) (deftest test-send ()