core/actor: example actor, with function mapping: calculator

This commit is contained in:
Helmut Merz 2025-04-13 15:13:54 +02:00
parent 1575040324
commit 4639108092
2 changed files with 21 additions and 12 deletions

View file

@ -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))

View file

@ -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 ()