Whoops, this belongs on ruby-talk... Sorry.
---------- Forwarded message ----------
From: Joe Van Dyk <joevandyk@gmail.com>
Date: Sep 26, 2005 6:20 PM
Subject: Lisp macros
To: rails@lists.rubyonrails.org
So, I''m diving into a little bit of Lisp, using
http://www.gigamonkeys.com/book/ as a starting point.
Lisp''s big selling point is macros, right? I''m only up to
chapter 3,
but from that (maybe very basic?) display of macros, it looks like
something that can be done in Ruby.
<excerpt>
(defun make-comparison-expr (field value)
`(equal (getf cd ,field) ,value))
(defun make-comparisons-list (fields)
(loop while fields
collecting (make-comparison-expr (pop fields) (pop fields))))
(defmacro where (&rest clauses)
`#''(lambda (cd) (and ,@(make-comparisons-list clauses))))
CL-USER> (macroexpand-1 ''(where :title "Give Us a Break"
:ripped t))
#''(LAMBDA (CD)
(AND (EQUAL (GETF CD :TITLE) "Give Us a Break")
(EQUAL (GETF CD :RIPPED) T)))
T
You could use eval + procs to do that, right?
Joe