开发者

Assign variable to multiple vectors in Clojure

开发者 https://www.devze.com 2023-04-10 15:03 出处:网络
The clojure.contrib.sql module has a create-table function that takes the table name and a list of specifications, like this:

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号