2017-03-02 14:26:47 -05:00
|
|
|
(defun counter (initial-count)
|
|
|
|
(let ((count initial-count))
|
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(dlambda
|
|
|
|
(:inc ()
|
|
|
|
(setq count (+ count 1)))
|
2017-03-02 14:26:47 -05:00
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(:dec ()
|
|
|
|
(setq count (- count 1)))
|
2017-03-02 14:26:47 -05:00
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(:get ()
|
|
|
|
count)
|
2017-03-02 14:26:47 -05:00
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(:set (value)
|
|
|
|
(setq 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
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(dlambda
|
|
|
|
(:inc-apples ()
|
|
|
|
(call apple-counter :inc))
|
2017-03-06 11:00:18 -05:00
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(:dec-apples ()
|
|
|
|
(call apple-counter :dec))
|
2017-03-06 11:00:18 -05:00
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(:get-apples ()
|
|
|
|
(call apple-counter :get))
|
2017-03-06 11:00:18 -05:00
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(:set-apples (value)
|
|
|
|
(call apple-counter :set value))
|
2017-03-06 11:00:18 -05:00
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(:inc-bananas ()
|
|
|
|
(call banana-counter :inc))
|
2017-03-06 11:00:18 -05:00
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(:dec-bananas ()
|
|
|
|
(call banana-counter :dec))
|
2017-03-06 11:00:18 -05:00
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(:get-bananas ()
|
|
|
|
(call banana-counter :get))
|
2017-03-06 11:00:18 -05:00
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(:set-bananas (value)
|
|
|
|
(call banana-counter :set value))
|
2017-03-06 11:00:18 -05:00
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(:inc-coconuts ()
|
|
|
|
(call coconut-counter :inc))
|
2017-03-06 11:00:18 -05:00
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(:dec-coconuts ()
|
|
|
|
(call coconut-counter :dec))
|
2017-03-06 11:00:18 -05:00
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(:get-coconuts ()
|
|
|
|
(call coconut-counter :get))
|
2017-03-06 11:00:18 -05:00
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(:set-coconuts (value)
|
|
|
|
(call coconut-counter :set value))
|
2017-03-06 11:00:18 -05:00
|
|
|
|
2017-03-10 15:08:42 -05:00
|
|
|
(t (&rest arguments)
|
|
|
|
(list
|
|
|
|
(list 'apples (call apple-counter :get))
|
|
|
|
(list 'bananas (call banana-counter :get))
|
|
|
|
(list 'coconuts (call 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-07 16:27:11 -05:00
|
|
|
; ~ (call my-fruits2 :set-apples 23)
|
2017-03-02 10:29:59 -05:00
|
|
|
; 23
|
|
|
|
;
|
2017-03-07 16:27:11 -05:00
|
|
|
(setq my-fruits2 (fruit-counter 10000))
|