transcendental-lisp/lisp/fruit-counter.lisp

120 lines
2.0 KiB
Common Lisp
Raw Normal View History

2017-03-01 16:45:48 -05:00
(load "dlambda.lisp")
2017-03-02 14:26:47 -05:00
(defun counter (initial-count)
(let ((count initial-count))
(eval
(dlambda
(:inc ()
(setf count (+ count 1))
)
(:dec ()
(setf count (- count 1))
)
(:get ()
count
)
(:set (value)
(setf count value)
)
)
)
)
)
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 ()
(funcall apple-counter :inc)
)
(:dec-apples ()
(funcall apple-counter :dec)
2017-03-02 10:29:59 -05:00
)
2017-03-02 14:26:47 -05:00
(:get-apples ()
(funcall apple-counter :get)
2017-03-02 10:29:59 -05:00
)
2017-03-02 14:26:47 -05:00
(:set-apples (value)
(funcall apple-counter :set value)
2017-03-02 10:29:59 -05:00
)
2017-03-02 14:26:47 -05:00
(:inc-bananas ()
(funcall banana-counter :inc)
2017-03-02 10:29:59 -05:00
)
2017-03-02 14:26:47 -05:00
(:dec-bananas ()
(funcall banana-counter :dec)
2017-03-02 10:29:59 -05:00
)
2017-03-02 14:26:47 -05:00
(:get-bananas ()
(funcall banana-counter :get)
2017-03-02 10:29:59 -05:00
)
2017-03-02 14:26:47 -05:00
(:set-bananas (value)
(funcall banana-counter :set value)
2017-03-02 10:29:59 -05:00
)
2017-03-02 14:26:47 -05:00
(:inc-coconuts ()
(funcall coconut-counter :inc)
2017-03-02 10:29:59 -05:00
)
2017-03-02 14:26:47 -05:00
(:dec-coconuts ()
(funcall coconut-counter :dec)
2017-03-02 10:29:59 -05:00
)
2017-03-02 14:26:47 -05:00
(:get-coconuts ()
(funcall coconut-counter :get)
2017-03-02 10:29:59 -05:00
)
2017-03-02 14:26:47 -05:00
(:set-coconuts (value)
(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
'apples (funcall apple-counter :get)
'bananas (funcall banana-counter :get)
'coconuts (funcall coconut-counter :get)
)
2017-03-02 10:29:59 -05:00
)
2017-03-01 16:45:48 -05:00
)
)
)
)
2017-03-02 10:29:59 -05:00
; Create an instance
;
; usage:
2017-03-02 14:26:47 -05:00
; ~ (my-counter :set-apples 23)
2017-03-02 10:29:59 -05:00
; 23
;
(let ((instance (fruit-counter 0)))
(defun my-counter (&rest args) (apply instance args))
)
; Another way
;
; usage:
2017-03-02 14:26:47 -05:00
; ~ (funcall my-counter2 :set-apples 23)
2017-03-02 10:29:59 -05:00
; 23
;
(setf my-counter2 (fruit-counter 10000))