Clojure:从匿名函数返回一个向量 [英] Clojure: returning a vector from an anonymous function
问题描述
我编写了一个小型匿名函数,用于 map
调用.该函数返回一个向量,其中包含来自 SQL 结果集查询的列名和列值.
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
这是map
调用:
(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?
谢谢.
推荐答案
你的问题是简单的语法试图将向量作为函数调用来求值.
Your problem is that the simple syntax is trying to evaluate the vector as a function call.
你可以插入一个identity"函数来让它工作,因为这只是一个简单的函数,它将返回不变的向量:
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 %)])
这篇关于Clojure:从匿名函数返回一个向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!