[英]How can I script math functionality in Clojure using Hiccup and Compojure?
我正在渲染一個home
,請求用戶輸入為整數向量 。 我需要這些數據結構,因為它們與我將用來操縱輸入的數學函數配合得很好:
(defn home [& [weights grades error]]
(html
;; ...
(form-to [:post "/"]
;; ...
(text-area {:rows 15 :cols 30 :placeholder
"[89 78 63]
[78 91 60]
[87 65 79]
..." } "grades" grades)]
(submit-button "process"))]))
和"process"
按鈕通過將輸入發送defroutes
功能,使用POST
方法,它調用一個processed
呈現方法html
表示從輸入計算結果。 用於計算最終等級列表的函數稱為process-grades
。 我正在嘗試使用read-string
將輸入數據結構更改為我的函數可以處理但無法正常工作的內容。 當我用"TEST"
代替對已processed
的調用時,在按下“ process
按鈕后呈現文本沒有問題:
(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))
(mapv #(apply + %))))
(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 (process-grades (read-string weights)(read-string grades))])))) ;; <- This is not working!
(defroutes grade-routes
(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")))))
我對html
form
標簽,Clojure的read-string
函數以及編寫所需腳本的各種方式進行了一些研究。 由於信息過多,我仍然想知道:什么是最簡單,最簡潔,慣用的方式? 我應該接觸Clojurescript還是在這里使用香草味的JVM Clojure?
您收到錯誤消息是因為(process-grades)
返回的是數字向量,這表示下面的形式
[:p (process-grades (read-string weights) (read-string grades))]
最終看起來將如下所示(一旦返回了process-grades
):
[:p [4/5 3/2 6/3 ... more numbers]]
打ic只知道如何在每個打vector矢量的開頭處處理關鍵字html標記,因此它會大聲抱怨。
最終,您需要以所需的方式很好地格式化輸出,但是暫時,您應該能夠通過將(process-grades ...)
調用包裝在(apply str)
來使向量運行,從而使其運行變成一個字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.