On Wed, Jun 1, 2011 at 12:35 AM, mmwaikar <mmwai...@gmail.com> wrote:
> Hi,
>
> I've to use clojure.contrib.sql's insert-records fn.
>
> Usage: (insert-records table & records)
>
> Inserts records into a table. records are maps from strings or
> keywords (identifying columns) to values.
>
> So, for ex. this works -
> (clojure.contrib.sql/insert-records :blogs
>                                               {:id 3 :title "third" :body 
> "third post"}
>                                               {:id 4 :title "fourth" :body 
> "fourth post"})
>
> but this doesn't -
> (clojure.contrib.sql/insert-records :blogs
>                                               ({:id 3 :title "third" :body 
> "third post"}
>                                               {:id 4 :title "fourth" :body 
> "fourth post"}))

How about

(apply insert-records :blogs
  [{:id 3 :title "third" :body "third post"}
   {:id 4 :title "fourth" :body "fourth post"}])

> Also, I get confused as to why some functions work on [] but not on lists
> ().

Lists are treated as function calls. You can quote a list to make it
be treated as a literal list, but embedded expressions will be
included as source code rather than evaluated. You can syntax
quote/unquote, which is cumbersome, or use the (list ...) function. Or
you can use a vector; the idiomatic thing is to use vector literals
for this sort of thing (sequential data embedded in the code).

=> (def x 42)
#'user/x
=> (2 x :q)
#<CompilerException java.lang.ClassCastException: java.lang.Integer
cannot be cast to clojure.lang.IFn (NO_SOURCE_FILE:0)>
=> '(2 x :q)
(2 x :q)
=> `(2 ~x :q)
(2 42 q)
=> (list 2 x :q)
(2 42 :q)
=> [2 x :q]
[2 x :q]

> In such cases, do I have to convert a list into a vector using something
> like vec?

If the list is generated by code, no; if it's literal, you may want to
just use a literal vector instead.

But you may need to use apply if the function takes variable arguments:

=> (+ 1 2 3 4 5)
15
=> (+ [1 2 3 4 5])
#<CompilerException java.lang.ClassCastException (NO_SOURCE_FILE:0)>
=> (apply + [1 2 3 4 5])
15

(Odd that that particular CCE doesn't specify the expected and found
types, like most CCEs...)

-- 
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to