transcendental-lisp/lisp/object/composition.lisp

89 lines
1.8 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-07 16:27:11 -05:00
(setq count (+ count 1)))
2017-03-02 14:26:47 -05:00
(:dec ()
2017-03-07 16:27:11 -05:00
(setq 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-07 16:27:11 -05:00
(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
(eval
(dlambda
2017-03-02 10:29:59 -05:00
2017-03-02 14:26:47 -05:00
(:inc-apples ()
2017-03-07 16:27:11 -05:00
(call apple-counter :inc))
2017-03-06 11:00:18 -05:00
2017-03-02 14:26:47 -05:00
(:dec-apples ()
2017-03-07 16:27:11 -05:00
(call apple-counter :dec))
2017-03-06 11:00:18 -05:00
2017-03-02 14:26:47 -05:00
(:get-apples ()
2017-03-07 16:27:11 -05:00
(call apple-counter :get))
2017-03-06 11:00:18 -05:00
2017-03-02 14:26:47 -05:00
(:set-apples (value)
2017-03-07 16:27:11 -05:00
(call apple-counter :set value))
2017-03-06 11:00:18 -05:00
2017-03-02 14:26:47 -05:00
(:inc-bananas ()
2017-03-07 16:27:11 -05:00
(call banana-counter :inc))
2017-03-06 11:00:18 -05:00
2017-03-02 14:26:47 -05:00
(:dec-bananas ()
2017-03-07 16:27:11 -05:00
(call banana-counter :dec))
2017-03-06 11:00:18 -05:00
2017-03-02 14:26:47 -05:00
(:get-bananas ()
2017-03-07 16:27:11 -05:00
(call banana-counter :get))
2017-03-06 11:00:18 -05:00
2017-03-02 14:26:47 -05:00
(:set-bananas (value)
2017-03-07 16:27:11 -05:00
(call banana-counter :set value))
2017-03-06 11:00:18 -05:00
2017-03-02 14:26:47 -05:00
(:inc-coconuts ()
2017-03-07 16:27:11 -05:00
(call coconut-counter :inc))
2017-03-06 11:00:18 -05:00
2017-03-02 14:26:47 -05:00
(:dec-coconuts ()
2017-03-07 16:27:11 -05:00
(call coconut-counter :dec))
2017-03-06 11:00:18 -05:00
2017-03-02 14:26:47 -05:00
(:get-coconuts ()
2017-03-07 16:27:11 -05:00
(call coconut-counter :get))
2017-03-06 11:00:18 -05:00
2017-03-02 14:26:47 -05:00
(:set-coconuts (value)
2017-03-07 16:27:11 -05:00
(call coconut-counter :set value))
2017-03-06 11:00:18 -05:00
2017-03-02 14:26:47 -05:00
(t (&rest arguments)
(list
2017-03-07 16:27:11 -05:00
(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))