[英]Clojure: map as function parameter
I'm trying to running some codes from the book "Web development with Clojure". 我正在尝试运行“使用Clojure进行网络开发”一书中的一些代码。 There is a function which I can not understand: 有一个我无法理解的功能:
(defn handle-upload [{:keys [filename] :as file}]
(upload-page
(if (empty? filename)
"please select a file to upload"
(try
(upload-file (gallery-path) file)
(save-thumbnail file)
(db/add-image (session/get :user) filename)
(image {:height "150px"}
(str "/img/"
(session/get :user)
"/"
thumb-prefix
(url-encode filename)))
(catch Exception ex
(str "error uploading file " (.getMessage ex)))))))
where 哪里
(defn upload-page [info]
(layout/common
[:h2 "Upload an image"]
[:p info]
(form-to {:enctype "multipart/form-data"}
[:post "/upload"]
(file-upload :file)
(submit-button "upload"))))
What is the meaning of the parameter of the function handle-upload
? 函数handle-upload
的参数是什么意思?
And after the changing from 而从
(defn handle-upload [{:keys [filename] :as file}] ...
to 至
(defn handle-upload [{:keys filename :as file}] ...
I got an error message: 我收到一条错误消息:
java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.Symbol, compiling:(picture_gallery/routes/upload.clj:32:1)
Why? 为什么?
{:keys [filename] :as file}
means: {:keys [filename] :as file}
意思是:
:filename
key from passed argument and bind its value to filename
从传递的参数中获取:filename
密钥,并将其值绑定到filename
file
将整个参数保留为file
So if you pass: 因此,如果您通过:
{:filename "foo"
:somethingelse "bar"}
As an argument, then filename
in the function scope will be equal to foo
and the file
will be equal to the whole hash map. 作为参数,函数范围内的filename
等于foo
, file
等于整个哈希图。
References: 参考文献:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.