Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.lisp

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 08-12-2006, 05:47 PM
Jonathon McKitrick
Guest
 
Posts: n/a
Default Macro help needed

Perhaps I'm totally going about this the wrong way.

I have a database query from clsql made with (multiple-value-list), so
it combines the matching record (there will be only one) with the
field-names:

( ( (val1 val2 val3) ) ( key1 key2 key3 ) )

What I want to do is write a macro that will accept the query and a
column name, and then execute the body with the corresponding value
bound to some variable. Not sure what variable name to choose, but
here's the feeble attempt at macro code:

(defmacro with-query-column (query column &body body)
"With a query of 1 result, get the value for a column."
(let ((val (gensym)))
`(destructuring-bind ((vals) columns) ,query
(let ((,val (elt vals (position ,column columns :test
#'equal))))
,@body))))

I'm sure I'm doing something wrong here. It just occurred to me I
might be better off building a hash table. What do you guys think?

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 08-12-2006, 06:28 PM
Barry Margolin
Guest
 
Posts: n/a
Default Re: Macro help needed

In article <1155404858.063567.24090@i42g2000cwa.googlegroups. com>,
"Jonathon McKitrick" <j_mckitrick@bigfoot.com> wrote:

> Perhaps I'm totally going about this the wrong way.
>
> I have a database query from clsql made with (multiple-value-list), so
> it combines the matching record (there will be only one) with the
> field-names:
>
> ( ( (val1 val2 val3) ) ( key1 key2 key3 ) )
>
> What I want to do is write a macro that will accept the query and a
> column name, and then execute the body with the corresponding value
> bound to some variable. Not sure what variable name to choose, but
> here's the feeble attempt at macro code:


Let the user supply the variable.

>
> (defmacro with-query-column (query column &body body)
> "With a query of 1 result, get the value for a column."
> (let ((val (gensym)))
> `(destructuring-bind ((vals) columns) ,query
> (let ((,val (elt vals (position ,column columns :test
> #'equal))))
> ,@body))))


You're using gensym for the wrong thing. You should be using it for the
temporary variables used internally within the expansion (vals and
columns), to avoid shadowing real variables, not for the variable that
you actually want the user to access within the body.

(defmacro with-query-column ((var column query) &body body)
(let ((vals (gensym))
(cols (gensym)))
`(destructuring-bind ((,vals) ,cols) ,query
(let ((,var (nth (position ,column ,cols :test #'equal) ,vals)))
,@body))))

> I'm sure I'm doing something wrong here. It just occurred to me I
> might be better off building a hash table. What do you guys think?


Unless these lists of values and keys are large, that's probably
overkill.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Reply With Quote
  #3 (permalink)  
Old 08-12-2006, 08:09 PM
Jonathon McKitrick
Guest
 
Posts: n/a
Default Re: Macro help needed


Barry Margolin wrote:
> You're using gensym for the wrong thing. You should be using it for the
> temporary variables used internally within the expansion (vals and
> columns), to avoid shadowing real variables, not for the variable that
> you actually want the user to access within the body.
>
> (defmacro with-query-column ((var column query) &body body)
> (let ((vals (gensym))
> (cols (gensym)))
> `(destructuring-bind ((,vals) ,cols) ,query
> (let ((,var (nth (position ,column ,cols :test #'equal) ,vals)))
> ,@body))))


Got it, thanks!

Is this violating the concept of using macros to define the problem,
not the solution?

Reply With Quote
  #4 (permalink)  
Old 08-12-2006, 08:49 PM
Jonathon McKitrick
Guest
 
Posts: n/a
Default Re: Macro help needed


Barry Margolin wrote:
> You're using gensym for the wrong thing. You should be using it for the
> temporary variables used internally within the expansion (vals and
> columns), to avoid shadowing real variables, not for the variable that
> you actually want the user to access within the body.
>
> (defmacro with-query-column ((var column query) &body body)
> (let ((vals (gensym))
> (cols (gensym)))
> `(destructuring-bind ((,vals) ,cols) ,query
> (let ((,var (nth (position ,column ,cols :test #'equal) ,vals)))
> ,@body))))


Got it, thanks!

Is this violating the concept of using macros to define the problem,
not the solution?

Reply With Quote
  #5 (permalink)  
Old 08-12-2006, 09:48 PM
Barry Margolin
Guest
 
Posts: n/a
Default Re: Macro help needed

In article <1155415775.499506.115000@74g2000cwt.googlegroups. com>,
"Jonathon McKitrick" <j_mckitrick@bigfoot.com> wrote:

> Barry Margolin wrote:
> > You're using gensym for the wrong thing. You should be using it for the
> > temporary variables used internally within the expansion (vals and
> > columns), to avoid shadowing real variables, not for the variable that
> > you actually want the user to access within the body.
> >
> > (defmacro with-query-column ((var column query) &body body)
> > (let ((vals (gensym))
> > (cols (gensym)))
> > `(destructuring-bind ((,vals) ,cols) ,query
> > (let ((,var (nth (position ,column ,cols :test #'equal) ,vals)))
> > ,@body))))

>
> Got it, thanks!
>
> Is this violating the concept of using macros to define the problem,
> not the solution?


Perhaps. It strikes me that this could just as easily be done with a
function:

(defun get-query-column (column query)
(destructuring-bind ((vals) cols) query
(nth (position column cols :test #'equal) vals)))

Then you can easily do:

(let ((thing (get-query-column 'col2 query)))
...)

WITH-XXX macros are usually necessary when you need to do something more
than just bind a variable, e.g. clean up things after the body is done.
For instance, WITH-OPEN-FILE has to close the file before finishing.

P.S. Why the double post? What is it about Google that causes this to
happen so often?

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Reply With Quote
  #6 (permalink)  
Old 08-13-2006, 12:53 AM
Jonathon McKitrick
Guest
 
Posts: n/a
Default Re: Macro help needed


Barry Margolin wrote:
> P.S. Why the double post? What is it about Google that causes this to
> happen so often?


Not sure. I hit the 'submit' button, and the progress bar sometimes
hangs 1/4 way across. Sometimes I just stop the browser and re-submit,
sometimes I hit 'preview' then re-submit from there. Other times I
force myself to be patient and let it do its thing, and I get a
time-out page and have to backup and resubmit anyway.

Reply With Quote
  #7 (permalink)  
Old 08-13-2006, 03:22 PM
bradb
Guest
 
Posts: n/a
Default Re: Macro help needed

Jonathon McKitrick wrote:
> Perhaps I'm totally going about this the wrong way.
>
> I have a database query from clsql made with (multiple-value-list), so
> it combines the matching record (there will be only one) with the
> field-names:
>
> ( ( (val1 val2 val3) ) ( key1 key2 key3 ) )
>
> What I want to do is write a macro that will accept the query and a
> column name, and then execute the body with the corresponding value
> bound to some variable. Not sure what variable name to choose, but
> here's the feeble attempt at macro code:
>
> (defmacro with-query-column (query column &body body)
> "With a query of 1 result, get the value for a column."
> (let ((val (gensym)))
> `(destructuring-bind ((vals) columns) ,query
> (let ((,val (elt vals (position ,column columns :test
> #'equal))))
> ,@body))))
>
> I'm sure I'm doing something wrong here. It just occurred to me I
> might be better off building a hash table. What do you guys think?

Jon, I tried to reply to your email but your bigfoot account bounced
me.

Brad

Reply With Quote
  #8 (permalink)  
Old 08-13-2006, 04:09 PM
jmckitrick
Guest
 
Posts: n/a
Default Re: Macro help needed


bradb wrote:
> Jon, I tried to reply to your email but your bigfoot account bounced
> me.


I've finally been forced to update my address. When the spam-bots find
me, I'll blame you. ;-P

Please try again, at the new one jmckitrick ( at ) yahoooooooo! ( dot )
KAHM.

Think that will throw them off?

Reply With Quote
  #9 (permalink)  
Old 08-13-2006, 04:11 PM
jmckitrick
Guest
 
Posts: n/a
Default Re: Macro help needed


bradb wrote:
> Jon, I tried to reply to your email but your bigfoot account bounced
> me.


I've finally been forced to update my address. When the spam-bots find
me, I'll blame you. ;-P

Please try again, at the new one jmckitrick ( at ) yahoooooooo! ( dot )
KAHM.

Think that will throw them off?

Reply With Quote
  #10 (permalink)  
Old 08-13-2006, 04:13 PM
jmckitrick
Guest
 
Posts: n/a
Default Re: Macro help needed


bradb wrote:
> Jon, I tried to reply to your email but your bigfoot account bounced
> me.


I've finally been forced to update my address. When the spam-bots find
me, I'll blame you. ;-P

Please try again, at the new one jmckitrick ( at ) yahoooooooo! ( dot )
KAHM.

Think that will throw them off?

Reply With Quote
  #11 (permalink)  
Old 08-13-2006, 08:10 PM
bradb
Guest
 
Posts: n/a
Default Re: Macro help needed


jmckitrick wrote:
> bradb wrote:
> > Jon, I tried to reply to your email but your bigfoot account bounced
> > me.

>
> I've finally been forced to update my address. When the spam-bots find
> me, I'll blame you. ;-P
>
> Please try again, at the new one jmckitrick ( at ) yahoooooooo! ( dot )
> KAHM.
>
> Think that will throw them off?


Hmm, that didn't work Just email me again from your new account.

Cheers
Brad

Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Understanding How Macro Processor Works Terjeson, Mark Newsgroup comp.soft-sys.sas 0 03-24-2006 11:00 PM
Re: MAcro Design was (Re: Macro quoting essentials) Ian Whitlock Newsgroup comp.soft-sys.sas 3 12-11-2005 11:18 PM
Re: Macro quoting essentials toby dunn Newsgroup comp.soft-sys.sas 1 12-08-2005 05:41 PM
Re: Weird macro quoting problem Joe Whitehurst Newsgroup comp.soft-sys.sas 0 04-23-2005 07:58 PM
Re: What's wrong with this macro? Thank you Ian Whitlock Newsgroup comp.soft-sys.sas 0 11-11-2004 08:29 PM



All times are GMT. The time now is 07:06 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.