[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
- To: djskrien@COLBY.EDU (Dale Skrien)
- Subject: Re: menu-item-actions
- From: Steve Strassmann <firstname.lastname@example.org>
- Date: Mon, 16 Mar 1992 17:30:55 -0500
- Cc: info-mcl
>You could also consider using the following code in
>the standard system:
> (setq fred (make-instance 'menu-item :menu-item-title "Fred"))
> (setf (menu-item-action-function fred)
> #'(lambda ()
> (print (menu-item-title fred))))
An alternative method:
You can also take advantage of the fact that lisp supports
the idea of a "closure", which means that when you define
a function, it also remembers what context it was in at the time.
This means you can write this code without having to resort
to yucky global variables.
Here, the locally bound ITEM is available to its own menu-action-function.
:menu-items ; this list contains just one value: ITEM
(let ((item (make-instance 'menu-item
:menu-item-title (format nil "Random [~a]"
(setf (menu-item-action-function item)
#'(lambda () (print (menu-item-title item))))
; Here's some more fun with closures:
(defun taxman-maker (rate)
#'(lambda (x) (* x (+ 1 rate))))
? (setq massachusetts (taxman-maker .15)) ; 15% tax rate
#<COMPILED-LEXICAL-CLOSURE #xED2F76> ; This is a function
? (funcall massachusetts 500)
? (mapcar massachusetts '(300 600 900))
(345.0 690.0 1035.0)