Re: access closure variable
- From: Rainer Joswig <joswig@xxxxxxx>
- Date: Wed, 09 Jul 2008 16:44:17 +0200
In article
<4546207c-336a-4ab2-95c3-a1d8a46aaa09@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Mirko.Vukovic@xxxxxxxxx wrote:
On Jul 8, 3:58 pm, Rainer Joswig <jos...@xxxxxxx> wrote:
In article
<73264c06-daa6-40ac-b480-a48cbbd24...@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Mirko.Vuko...@xxxxxxxxx wrote:
On Jul 7, 3:06 am, Rainer Joswig <jos...@xxxxxxx> wrote:
In article
<472fab77-164b-466b-9f5a-92b4cb845...@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
gtasso <geta...@xxxxxxxxx> wrote:
Hello all,
consider this..
(setf foo '"hello world")
I could access the the value this way
(symbol-value'foo)
wonder if i could do something like this with variable setup in a
closure like this.
(let ((foo "hello world)
(bar "hello c.lang.lisp"))
(defun get-value (lst)
(dolist (e lst)
(print (symbol-valuee))))
and from outside let i issue
(get-value '(foo bar)) - > i get error variable unbound
Because you can't accesslexicalvariable values
withSYMBOL-VALUE.
I could write individual function for each variables in the closure
but I am looking for shorter way.
CL-USER 9 > (mapcar (let ((a 1) (b 2))
(flet ((get-value (sym)
(ecase sym
(a a)
(b b))))
#'get-value))
'(b a))
(2 1)
I could write the whole thing as an object and benefit from the
accessor and slot-value function but i guess i wanted it done with
closure instead :).
Many thanks.
regards,
George.
--http://lispm.dyndns.org/
This group is positively possessed by super-unnatural forces. So
often it happens that as I type a question, the answer just
materializes to me. And now, my question was answered several days
before it became even actual!
So, here is a macro with which I tried to automate the OP's quest for
automation. I am trying to re-learn to program without objects.
(in-package :my-utils)
(defmacro with-visible-lexs (visible-lexs &optional init-block &body
cases)
"Return function (case-based) with closure & with utility to view
lexical values"
`(let ,visible-lexs
,(when init-block init-block)
(lambda (command &optional args)
(case command
(prop
(ecase args
,@(loop for lex in visible-lexs
collect `(,lex ,lex))))
,@cases))))
Here is a sample session:
MY-UTILS-UNIT-TESTS> (setf *a* (foo 1 3))
#<CLOSURE (SB-C::&OPTIONAL-DISPATCH (LAMBDA #)) {100366F169}>
MY-UTILS-UNIT-TESTS> (apply *a* (list 'sum))
8
MY-UTILS-UNIT-TESTS> (apply *a* (list 'mult))
12
MY-UTILS-UNIT-TESTS> (apply *a* (list 'my-utils::prop 'c))
4
One thing I don't know how to deal with is that the macro uses the
symbol my-utils::prop, since that is where it sits. I don't know how
to go around that.
If anyone has a suggestion for a better name(s), by all means.
Inevitable improvements & suggestions, also (I have not really thought
out if it needs gensym - args & lex may need to be).
Mirko
Thanks Rainer,
Flavors uses
What is Flavors? Could not find it on cliki or the web.
http://en.wikipedia.org/wiki/Flavors_%28programming_language%29
Flavors was/is an early extension to Lisp for object-oriented programming.
ftp://publications.ai.mit.edu/ai-publications/pdf/AIM-602.pdf
http://www.softwarepreservation.org/projects/LISP/rutgers/clisp/flavors.pdf
It was oriented towards 'message passing'. Later developed into New Flavors,
which had some influence in the design of CLOS.
(send some-window :set-edges 10 10 40 40)
So, use keyword symbols. Use &rest args instead of optional args.
Then you have
(funcall *a* :prop 'c)
Cool. I keep forgetting about keywords and their package independence
(not cool).
Keywords are not really package independent. Keywords are in the package
"KEYWORD".
CL-USER 1 > (symbol-package :foo)
#<The KEYWORD package, 0/4 internal, 5302/8192 external>
CL-USER 2 > KEYWORD::FOO
:FOO
CL-USER 3 > KEYWORD:FOO
:FOO
CL-USER 4 > :FOO
:FOO
CL-USER 5 > ::FOO
:FOO
The reader will read :foo as a symbol in the keyword package.
The package is missing in front of the : and the symbol, so it will
be the KEYWORD package by default.
In good old style replace funcall with send:
(send *a* :prop 'c)
;-)
--http://lispm.dyndns.org/
"send"? I do not find it in hyperspec. Do you mean as an alias for
funcall?
Yes.
Thanks,
Mirko
--
http://lispm.dyndns.org/
.
- References:
- access closure variable
- From: gtasso
- Re: access closure variable
- From: Rainer Joswig
- Re: access closure variable
- From: Mirko . Vukovic
- Re: access closure variable
- From: Rainer Joswig
- Re: access closure variable
- From: Mirko . Vukovic
- access closure variable
- Prev by Date: Re: Good Lisp editor for Win
- Next by Date: Re: access closure variable
- Previous by thread: Re: access closure variable
- Next by thread: Re: access closure variable
- Index(es):
Relevant Pages
|