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)
 | 
					  (:use :common-lisp)
 | 
				
			||||||
  (:local-nicknames (:util :scopes/util))
 | 
					  (:local-nicknames (:util :scopes/util))
 | 
				
			||||||
  (:export #:actor #:become #:create #:send
 | 
					  (:export #:actor #:become #:create #:send
 | 
				
			||||||
           #:echo #:inc #:lgi))
 | 
					           #:echo #:inc #:lgi
 | 
				
			||||||
 | 
					           #:calculator #:plus #:minus #:show))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(in-package :scopes/core/actor)
 | 
					(in-package :scopes/core/actor)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,7 +15,7 @@
 | 
				
			||||||
  ((content :reader content :initarg :content :initform nil)
 | 
					  ((content :reader content :initarg :content :initform nil)
 | 
				
			||||||
   (customer :reader customer :initarg :customer :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))
 | 
					  (make-instance 'message :content content :customer customer))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defclass actor ()
 | 
					(defclass actor ()
 | 
				
			||||||
| 
						 | 
					@ -46,9 +47,17 @@
 | 
				
			||||||
(defun echo (ac msg)
 | 
					(defun echo (ac msg)
 | 
				
			||||||
  (send (customer msg) msg))
 | 
					  (send (customer msg) msg))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defun inc (&optional (val 0))
 | 
					;;;; example behavior: calculator
 | 
				
			||||||
  #'(lambda (ac msg) 
 | 
					
 | 
				
			||||||
      (let ((c (content msg))) 
 | 
					(defun calculator (&optional (val 0))
 | 
				
			||||||
        (if (eq c :show)
 | 
					  #'(lambda (ac msg)
 | 
				
			||||||
          (send (create #'lgi) val)
 | 
					      (destructuring-bind (fn &optional param) (content msg)
 | 
				
			||||||
          (become ac (inc (+ c val)))))))
 | 
					        (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 ()
 | 
					(deftest test-actor ()
 | 
				
			||||||
  (let (a1 a2 a3)
 | 
					  (let (a1 a2 a3)
 | 
				
			||||||
    (setf a1 (actor:create (actor:inc)))
 | 
					    (setf a1 (actor:create (actor:calculator)))
 | 
				
			||||||
    (actor:send a1 2)
 | 
					    (actor:send a1 '(actor:plus 2))
 | 
				
			||||||
    (actor:send a1 3)
 | 
					    (actor:send a1 '(actor:minus 3))
 | 
				
			||||||
    (actor:send a1 :show)
 | 
					    (actor:send a1 '(actor:show))
 | 
				
			||||||
    ))
 | 
					    ))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(deftest test-send ()
 | 
					(deftest test-send ()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue