core/actor: example actor, with function mapping: calculator
This commit is contained in:
parent
1575040324
commit
4639108092
2 changed files with 21 additions and 12 deletions
|
@ -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))
|
||||
;;;; example behavior: calculator
|
||||
|
||||
(defun calculator (&optional (val 0))
|
||||
#'(lambda (ac msg)
|
||||
(let ((c (content msg)))
|
||||
(if (eq c :show)
|
||||
(send (create #'lgi) val)
|
||||
(become ac (inc (+ c val)))))))
|
||||
(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))
|
||||
|
||||
|
|
|
@ -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 ()
|
||||
|
|
Loading…
Add table
Reference in a new issue