71 lines
1.1 KiB
Common Lisp
71 lines
1.1 KiB
Common Lisp
|
(load "dlambda.lisp")
|
||
|
|
||
|
(defun counter (initial-count)
|
||
|
(let ((count initial-count)
|
||
|
(print-prefix nil)
|
||
|
(this nil))
|
||
|
|
||
|
(setf print-prefix "Counter")
|
||
|
|
||
|
(setf this
|
||
|
(eval
|
||
|
(dlambda
|
||
|
|
||
|
(:inc ()
|
||
|
(setf count (+ count 1))
|
||
|
)
|
||
|
|
||
|
(:inc-3 ()
|
||
|
(funcall this :inc)
|
||
|
(funcall this :inc)
|
||
|
(funcall this :inc)
|
||
|
)
|
||
|
|
||
|
(:dec ()
|
||
|
(setf count (- count 1))
|
||
|
)
|
||
|
|
||
|
(:dec-3 ()
|
||
|
(funcall this :dec)
|
||
|
(funcall this :dec)
|
||
|
(funcall this :dec)
|
||
|
)
|
||
|
|
||
|
(:get ()
|
||
|
count
|
||
|
)
|
||
|
|
||
|
(:set (value)
|
||
|
(setf count value)
|
||
|
)
|
||
|
|
||
|
(t ()
|
||
|
(cons print-prefix count)
|
||
|
)
|
||
|
|
||
|
)
|
||
|
)
|
||
|
)
|
||
|
|
||
|
)
|
||
|
)
|
||
|
|
||
|
; Create an instance
|
||
|
;
|
||
|
; usage:
|
||
|
; ~ (my-counter :inc-3)
|
||
|
; 3
|
||
|
;
|
||
|
(let ((instance (counter 0)))
|
||
|
(defun my-counter (&rest args) (apply instance args))
|
||
|
)
|
||
|
|
||
|
; Another way
|
||
|
;
|
||
|
; usage:
|
||
|
; ~ (funcall my-counter2 :dec-3)
|
||
|
; 997
|
||
|
;
|
||
|
(setf my-counter2 (counter 10000))
|
||
|
|