Clojure: map as function parameter -
i'm trying running codes book "web development clojure". there function can not understand:
(defn handle-upload [{:keys [filename] :as file}] (upload-page (if (empty? filename) "please select file 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 image"] [:p info] (form-to {:enctype "multipart/form-data"} [:post "/upload"] (file-upload :file) (submit-button "upload"))))
what meaning of parameter of function handle-upload
?
and after changing from
(defn handle-upload [{:keys [filename] :as file}] ...
to
(defn handle-upload [{:keys filename :as file}] ...
i got error message:
java.lang.illegalargumentexception: don't know how create iseq from: clojure.lang.symbol, compiling:(picture_gallery/routes/upload.clj:32:1)
why?
{:keys [filename] :as file}
means:
- take
:filename
key passed argument , bind valuefilename
- leave whole argument available
file
so if pass:
{:filename "foo" :somethingelse "bar"}
as argument, filename
in function scope equal foo
, file
equal whole hash map.
references:
Comments
Post a Comment