Clojure: returning a vector from an anonymous function

后端 未结 3 1958
盖世英雄少女心
盖世英雄少女心 2020-12-14 17:14

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 que

相关标签:
3条回答
  • 2020-12-14 17:25

    You need to use vector function to do this:

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

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

    0 讨论(0)
  • 2020-12-14 17:36

    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 %)])
    
    0 讨论(0)
  • 2020-12-14 17:43

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

    I would recommend the following as the best alternative:

    #(vector (keyword %) (.getObject resultset %))
    
    0 讨论(0)
提交回复
热议问题