transcendental-lisp/lisp/object/fruit-counter.lisp

101 lines
1.9 KiB
Common Lisp
Raw Normal View History

2017-03-06 11:00:18 -05:00
(load "../lang/dlambda.lisp")
2017-03-01 16:45:48 -05:00
2017-03-02 14:26:47 -05:00
(defun counter (initial-count)
(let ((count initial-count))
(eval
(dlambda
(:inc ()
2017-03-06 11:00:18 -05:00
(setf count (+ count 1)))
2017-03-02 14:26:47 -05:00
(:dec ()
2017-03-06 11:00:18 -05:00
(setf count (- count 1)))
2017-03-02 14:26:47 -05:00
(:get ()
2017-03-06 11:00:18 -05:00
count)
2017-03-02 14:26:47 -05:00
(:set (value)
2017-03-06 11:00:18 -05:00
(setf count value))))))
2017-03-02 14:26:47 -05:00
2017-03-01 16:45:48 -05:00
(defun fruit-counter (initial-count)
2017-03-02 14:26:47 -05:00
(let ((apple-counter (counter initial-count))
(banana-counter (counter initial-count))
(coconut-counter (counter initial-count)))
2017-03-01 16:45:48 -05:00
(eval
(dlambda
2017-03-02 10:29:59 -05:00
2017-03-02 14:26:47 -05:00
(:inc-apples ()
2017-03-06 11:00:18 -05:00
(funcall apple-counter :inc))
2017-03-02 14:26:47 -05:00
(:dec-apples ()
2017-03-06 11:00:18 -05:00
(funcall apple-counter :dec))
2017-03-02 10:29:59 -05:00
2017-03-02 14:26:47 -05:00
(:get-apples ()
2017-03-06 11:00:18 -05:00
(funcall apple-counter :get))
2017-03-02 10:29:59 -05:00
2017-03-02 14:26:47 -05:00
(:set-apples (value)
2017-03-06 11:00:18 -05:00
(funcall apple-counter :set value))
2017-03-02 10:29:59 -05:00
2017-03-02 14:26:47 -05:00
(:inc-bananas ()
2017-03-06 11:00:18 -05:00
(funcall banana-counter :inc))
2017-03-02 10:29:59 -05:00
2017-03-02 14:26:47 -05:00
(:dec-bananas ()
2017-03-06 11:00:18 -05:00
(funcall banana-counter :dec))
2017-03-02 10:29:59 -05:00
2017-03-02 14:26:47 -05:00
(:get-bananas ()
2017-03-06 11:00:18 -05:00
(funcall banana-counter :get))
2017-03-02 10:29:59 -05:00
2017-03-02 14:26:47 -05:00
(:set-bananas (value)
2017-03-06 11:00:18 -05:00
(funcall banana-counter :set value))
2017-03-02 10:29:59 -05:00
2017-03-02 14:26:47 -05:00
(:inc-coconuts ()
2017-03-06 11:00:18 -05:00
(funcall coconut-counter :inc))
2017-03-02 10:29:59 -05:00
2017-03-02 14:26:47 -05:00
(:dec-coconuts ()
2017-03-06 11:00:18 -05:00
(funcall coconut-counter :dec))
2017-03-02 10:29:59 -05:00
2017-03-02 14:26:47 -05:00
(:get-coconuts ()
2017-03-06 11:00:18 -05:00
(funcall coconut-counter :get))
2017-03-02 10:29:59 -05:00
2017-03-02 14:26:47 -05:00
(:set-coconuts (value)
2017-03-06 11:00:18 -05:00
(funcall coconut-counter :set value))
2017-03-02 10:29:59 -05:00
2017-03-02 14:26:47 -05:00
(t (&rest arguments)
(list
2017-03-03 15:06:49 -05:00
(list 'apples (funcall apple-counter :get))
(list 'bananas (funcall banana-counter :get))
2017-03-06 11:00:18 -05:00
(list 'coconuts (funcall coconut-counter :get))))))))
2017-03-02 10:29:59 -05:00
; Create an instance
2017-03-06 11:00:18 -05:00
;
; usage:
2017-03-03 15:06:49 -05:00
; ~ (my-fruits :set-apples 23)
2017-03-02 10:29:59 -05:00
; 23
;
(let ((instance (fruit-counter 0)))
2017-03-06 11:00:18 -05:00
(defun my-fruits (&rest args) (apply instance args)))
2017-03-02 10:29:59 -05:00
; Another way
2017-03-06 11:00:18 -05:00
;
; usage:
2017-03-03 15:06:49 -05:00
; ~ (funcall my-fruits2 :set-apples 23)
2017-03-02 10:29:59 -05:00
; 23
;
2017-03-03 15:06:49 -05:00
(setf my-fruits2 (fruit-counter 10000))