Assign variable to multiple vectors in Clojure
The clojure.contrib.sql
module has a create-table
function that takes the table name and a list of specifications, like this:
(sql/create-table :services
[:id :serial "PRIMARY KEY"]
[:service_name :varchar "NOT NULL"]
[:pass_hash :varchar "NOT NULL"]
[:token :varchar "NOT NULL"])
If I'm reusing the same columns again and again, is there a way to define something like this?
(def same-columns
[:id :serial "PRIMARY KEY"]
[:service_name :varchar "NOT NULL"]
[:pass_hash :varchar "NOT NULL"]
开发者_如何学JAVA [:token :varchar "NOT NULL"])
When I tried running that in the REPL I got an error, because it passes too many arguments to def
.
You could probably use apply for this:
(def same-columns [[:id :serial "PRIMARY KEY"]
[:service_name :varchar "NOT NULL"]
[:pass_hash :varchar "NOT NULL"]
[:token :varchar "NOT NULL"]])
(apply sql/create-table
:services
same-columns)
If you have other columns you can add those as well:
(apply sql/create-table
:services
[:some-column :varchar "NOT NULL"]
same-columns)
精彩评论