Expanded dlambda example some more
This commit is contained in:
parent
d55ebdd22f
commit
4a91d197fd
|
@ -1,8 +1,9 @@
|
|||
(defun map (fn ls)
|
||||
(defun mapcar (fn ls)
|
||||
(if (null ls)
|
||||
()
|
||||
(cons (funcall fn (first ls))
|
||||
(map fn (rest ls))
|
||||
(cons
|
||||
(funcall fn (first ls))
|
||||
(mapcar fn (rest ls))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
@ -17,7 +18,7 @@
|
|||
(quote case)
|
||||
(cons
|
||||
(quote (first arguments))
|
||||
(map
|
||||
(mapcar
|
||||
(lambda (method)
|
||||
(cons
|
||||
(first method)
|
||||
|
@ -29,7 +30,12 @@
|
|||
(quote lambda)
|
||||
(rest method)
|
||||
)
|
||||
(list (quote (rest arguments)))
|
||||
(list
|
||||
(if (equal t (car method))
|
||||
(quote arguments)
|
||||
(quote (rest arguments))
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
|
|
@ -1,60 +1,96 @@
|
|||
(load "dlambda.lisp")
|
||||
|
||||
(defun fruit-counter (initial-count)
|
||||
|
||||
(let ((apple-count initial-count)
|
||||
(banana-count initial-count)
|
||||
(coconut-count initial-count))
|
||||
(defun counter (initial-count)
|
||||
(let ((count initial-count))
|
||||
|
||||
(eval
|
||||
(dlambda
|
||||
|
||||
(:inc-apple ()
|
||||
(setf apple-count (+ apple-count 1))
|
||||
(:inc ()
|
||||
(setf count (+ count 1))
|
||||
)
|
||||
|
||||
(:dec-apple ()
|
||||
(setf apple-count (- apple-count 1))
|
||||
(:dec ()
|
||||
(setf count (- count 1))
|
||||
)
|
||||
|
||||
(:get-apple ()
|
||||
apple-count
|
||||
(:get ()
|
||||
count
|
||||
)
|
||||
|
||||
(:set-apple (value)
|
||||
(setf apple-count value)
|
||||
(:set (value)
|
||||
(setf count value)
|
||||
)
|
||||
|
||||
(:inc-banana ()
|
||||
(setf banana-count (+ banana-count 1))
|
||||
)
|
||||
)
|
||||
|
||||
(:dec-banana ()
|
||||
(setf banana-count (- banana-count 1))
|
||||
)
|
||||
)
|
||||
|
||||
(defun fruit-counter (initial-count)
|
||||
|
||||
(let ((apple-counter (counter initial-count))
|
||||
(banana-counter (counter initial-count))
|
||||
(coconut-counter (counter initial-count)))
|
||||
|
||||
(eval
|
||||
(dlambda
|
||||
|
||||
(:inc-apples ()
|
||||
(funcall apple-counter :inc)
|
||||
)
|
||||
|
||||
(:get-banana ()
|
||||
banana-count
|
||||
(:dec-apples ()
|
||||
(funcall apple-counter :dec)
|
||||
)
|
||||
|
||||
(:set-banana (value)
|
||||
(setf banana-count value)
|
||||
(:get-apples ()
|
||||
(funcall apple-counter :get)
|
||||
)
|
||||
|
||||
(:inc-coconut ()
|
||||
(setf coconut-count (+ coconut-count 1))
|
||||
(:set-apples (value)
|
||||
(funcall apple-counter :set value)
|
||||
)
|
||||
|
||||
(:dec-coconut ()
|
||||
(setf coconut-count (- coconut-count 1))
|
||||
(:inc-bananas ()
|
||||
(funcall banana-counter :inc)
|
||||
)
|
||||
|
||||
(:get-coconut ()
|
||||
coconut-count
|
||||
(:dec-bananas ()
|
||||
(funcall banana-counter :dec)
|
||||
)
|
||||
|
||||
(:set-coconut (value)
|
||||
(setf coconut-count value)
|
||||
(:get-bananas ()
|
||||
(funcall banana-counter :get)
|
||||
)
|
||||
|
||||
(:set-bananas (value)
|
||||
(funcall banana-counter :set value)
|
||||
)
|
||||
|
||||
(:inc-coconuts ()
|
||||
(funcall coconut-counter :inc)
|
||||
)
|
||||
|
||||
(:dec-coconuts ()
|
||||
(funcall coconut-counter :dec)
|
||||
)
|
||||
|
||||
(:get-coconuts ()
|
||||
(funcall coconut-counter :get)
|
||||
)
|
||||
|
||||
(:set-coconuts (value)
|
||||
(funcall coconut-counter :set value)
|
||||
)
|
||||
|
||||
(t (&rest arguments)
|
||||
(list
|
||||
'apples (funcall apple-counter :get)
|
||||
'bananas (funcall banana-counter :get)
|
||||
'coconuts (funcall coconut-counter :get)
|
||||
)
|
||||
)
|
||||
|
||||
)
|
||||
|
@ -66,7 +102,7 @@
|
|||
; Create an instance
|
||||
;
|
||||
; usage:
|
||||
; ~ (my-counter :set-apple 23)
|
||||
; ~ (my-counter :set-apples 23)
|
||||
; 23
|
||||
;
|
||||
(let ((instance (fruit-counter 0)))
|
||||
|
@ -76,7 +112,7 @@
|
|||
; Another way
|
||||
;
|
||||
; usage:
|
||||
; ~ (funcall my-counter2 :set-apple 23)
|
||||
; ~ (funcall my-counter2 :set-apples 23)
|
||||
; 23
|
||||
;
|
||||
(setf my-counter2 (fruit-counter 10000))
|
||||
|
|
Loading…
Reference in New Issue