25

I wrote a small anonymous function to be used with a map call. The function returns a vector containing a column name and column value from a SQL result set query.

Here is the function (input is the column name):

(fn [name] [(keyword name) (.getObject resultset name)])

This works fine, however when I tried to use a "simplified" version of the anonymous function, I got an error:

#([(keyword %) (.getObject resultset %)])

java.lang.IllegalArgumentException: Wrong number of args (0) passed to: PersistentVector

Here is the map call:

(into {} (map (fn [name] [(keyword name) (.getObject resultset name)]) column-names))

Is it possible to use the simplified syntax for this function? If so, how?

Thanks.

Ralph
  • 31,584
  • 38
  • 145
  • 282
  • Some more information: http://groups.google.com/group/clojure/browse_thread/thread/9037f31748cef0e2/62d3f3a45a139157?lnk=gst&q=Nick+Zbinden#62d3f3a45a139157 – nickik Feb 07 '11 at 13:26

3 Answers3

29

Your problem is that the simple syntax is trying to evaluate the vector as a function call.

You can insert an "identity" function to make it work, as this is just a simple function that will return the vector unchanged:

#(identity [(keyword %) (.getObject resultset %)])
mikera
  • 105,238
  • 25
  • 256
  • 415
  • I chose this answer because it also explains why the problem occurs, although the answer by Alex Ott also works (and provides a general solution for other data structures). Thanks, all. – Ralph Feb 07 '11 at 13:20
  • I edited this answer to incorporate the Alex's suggestion, but it looks like it was ignored for some reason. Oh well. – apg Feb 07 '11 at 18:07
  • I added an example of the more ideal solution rather than just showing code that demonstrates why it wasn't working. Since you edit didn't take. – Psyllo Aug 01 '11 at 20:53
  • 1
    Using `#(do [(keyword %) (.getObject resultset %)])` is shorter and probably a little faster than using `identity` – Nahuel Greco Dec 28 '20 at 21:18
27

You need to use vector function to do this:

#(vector (keyword %) (.getObject resultset %))

P.S. there are also functions for maps, sets, etc.

Alex Ott
  • 80,552
  • 8
  • 87
  • 132
5

Yeah, Clojure should really support a #[...] construct, just for this case.

I would recommend the following as the best alternative:

#(vector (keyword %) (.getObject resultset %))
drcode
  • 3,287
  • 2
  • 25
  • 29
  • 2
    I don't think #[...] would be common enough to warrant special syntax for it, and once you learn about it, throwing `vector` in there isn't a big deal. – apg Feb 07 '11 at 15:19
  • I thought of the same `#[...]` construct. How could I write a macro for it? # means reader macro right? – Petrus Theron Dec 02 '17 at 13:26
  • You can write `#(-> [(keyword %) (.getObject resultset %)])`, but I find this unclear – Petrus Theron Dec 02 '17 at 13:26