Hi,
On Feb 4, 4:42 am, Greg <[email protected]> wrote:
> The magic happens here:
>
> (while (list? (setf values (sql:next-row)))
> (eval (expand (cons 'begin $args) (unify keys
> values)))
> )
>
> $args is equivalent to the 'body' in [& body] in clojure, and the expand
> function will replace all of the symbols in 'keys' (in $args) with the
> 'values'.
>
> So (println NAME) becomes (println "José Lopez"), and it gets eval'd each
> time in the loop to the different values in the table.
>
> How would you implement this behavior in Clojure?
(defn do-query
[db query thunk]
(doseq [entry (-> db (.prepareStatement query) .executeQuery
resultset-seq)]
(thunk entry)))
(let [q "SELECT name FROM people"]
(do-query db q #(println (:name %))))
Several differences to the newLisp code:
* It's a function, not macro.
* You don't have to fiddle with magic names. The user can choose
himself.
* The query can be also stored in some local and doesn't have to be a
literal
string.
I am really happy that I'm a smug Clojure weenie, who doesn't have to
write
code which walks other code to replace things in place and to eval
it...
Sincerely
Meikel
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en