我正在制作一个 Clojure 网络应用程序来处理数字输入数据。我已经设置了主页,并且正在处理输入的数据并在通过按钮提交后显示。
带有注释的关键代码位:
(defn process-grades
"Takes user input from home's form-to function and processes it into the final grades list"
[weights grades]
(->> grades
(map (partial percentify-vector weights)) ;; <- The functions being called here are omitted but defined in the same name space.
(mapv #(apply + %))))
(defn home [& [weights grades error]]
(html
[:head
[:title "Home | Clojuregrade"]]
[:body
[:h1 "Welcome to Clojuregrade"]
[:p error]
[:hr]
(form-to [:post "/"]
[:h3 "Enter the weights for each of the grades below. Of course, all of the numbers should add up to 100%. Be sure to include the brackets"
[:br]
(text-area {:cols 30 :placeholder "[40 10 50] <- adds up to 100%"} "weights" weights)]
[:h3 "Enter ALL of the grades for EACH STUDENT in your class.
Make sure that each of the grades is ordered such that the grade corresponds
to its matching weight above."
[:br]
(text-area {:rows 15 :cols 30 :placeholder
"[89 78 63]
[78 91 60]
[87 65 79]
... " } "grades" grades)]
(submit-button "process"))]))
(defn processed [weights grades]
(cond
(empty? weights)
(home weights grades "You forgot to add the weights!")
(empty? grades)
(home weights grades "You forgot to add the grades!")
:else
(do
(html
[:h2 "These are your final grades."]
[:hr]
[:p "test"])))) ;; <- I would like to call process-grades here. "test" renders fine.
(defroutes app
(GET "/" []
{:status 200
:headers {"Content-Type" "text/html"}
:body (home)})
(POST "/" [weights grades] (processed weights grades))
(ANY "*" []
(route/not-found (slurp (io/resource "404.html")))))
;; ...