Subject: Re: (cached) named return values?
From: rpw3@rpw3.org (Rob Warnock)
Date: Sun, 04 Nov 2007 03:34:21 -0600
Newsgroups: comp.lang.lisp
Message-ID: <WMCdnXt_WYOAD7DanZ2dnUVZ_gKdnZ2d@speakeasy.net>
Pascal Costanza  <pc@p-cos.net> wrote:
+---------------
| As to your original question, when a function returns a property list, 
| you can conveniently retrieve variable bindings using lambda or 
| destructuring-bind:
| 
| (destructuring-bind
|    (&key a b c) (some-function ...)
|    ... some code ...)
| 
| or:
| 
| (apply (lambda (&key a b c)
|           ... some code ...)
|    (some-function ...))
+---------------

Nice. The GET-PROPERTIES standard function will do part of this
for you [looking for multiple indicators at once], but you have
to write a loop around it to get the same results as the above.

Caveat: To use the above in the general case, some additional
constraints are needed:

1. Either: (a) the property lists SOME-FUNCTION returns need
   to use keywords as markers, not just any old symbols; or else
   (b) each parameter X in the lambda lists must be re-written
   as ((X X)) [rather ugly, IMHO, but does work].

2. If you're only binding a few of the indicators in the supplied
   property list, your lambda list also needs &ALLOW-OTHER-KEYS.

But that said, it is a neat hack, well worth remembering:

    > (defun some-function ()
	(list :x 0 :c 3 :y 234 :b 2 :sldk 34 :a 1 :kdjf 12))

    SOME-FUNCTION
    > (destructuring-bind (&key a b c &allow-other-keys)
	  (some-function)
	(list a b c))

    (1 2 3)
    > (apply (lambda (&key a b c &allow-other-keys)
	       (list b c a))
	     (some-function))

    (2 3 1)
    > 

And if you really need non-keyword indicators:

    > (defun some-other-function ()
	(list 'x 0 'c 6 'y 234 'b 5 'sldk 34 'a 4 'kdjf 12))

    SOME-OTHER-FUNCTION
    > (destructuring-bind (&key ((a a)) ((b b)) ((c c)) &allow-other-keys)
	  (some-other-function)
	(list a b c))

    (4 5 6)
    > (apply (lambda (&key ((a a)) ((b b)) ((c c)) &allow-other-keys)
	       (list b c a))
	     (some-other-function))

    (5 6 4)
    > 


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607