0

I am making a simple web-app to help my teacher friends calculate their grades. I have the current bit of code that I am working with below:

    (defn home [& [weights grades error]] 
      (layout/common
        [:h1 "Welcome to Clojure-grade"]

        [:hr]

        (form-to [:post "/"]

                 [:p "Enter the weights for your various grades below. 
                  Of course, all of the numbers should add up to 100%, as in the example, below." 
                  (text-field {:placeholder "40 10 50"} "weights" weights)]

                 [:p "Enter all of the grades for each student. 
                  Make sure that each of the grades is ordered to correspond 
                  to its matching weight above. use brackets to separate students from each other. 
                  The following example shows grades for 4 students. Format your grades according to         
                  the number of students in your class:" 
                  (text-area {:rows 40 :cols 40 :placeholder  
                              "[89 78 63]
                               [78 91 79]
                               [54 85 91]
                              ..."  } "grades" grades)]
                 (submit-button "process"))))

(defn process-grades [weights grades]
    (->> (float grades)
         (map (partial percentify-vector (float weights)))
         (mapv #(apply + %))))

(defroutes app
  (GET "/" []
       {:status 200
        :headers {"Content-Type" "text/html"}
        :body home})
  (POST "/" [weights grades] (process-grades weights grades))
  (ANY "*" []
       (route/not-found (slurp (io/resource "404.html")))))

(defn wrap-error-page [handler]
  (fn [req]
    (try (handler req)
         (catch Exception e
           {:status 500
            :headers {"Content-Type" "text/html"}
            :body (slurp (io/resource "500.html"))}))))

I am guessing that the data will be bound to a the corresponding weights and grades symbols as strings. I need to pop off those quotation marks to use floats and vectors in my calculating functions, however. How can I do this? I am a beginner at this, too, so if there are any mistakes in my code, or I am going about things the wrong way, please let me know. Also, if you need more name-space or project.clj info, ask and I will expand.

4

1 回答 1

0

您可以使用 java interop 将字符串转换为浮点数或整数,但惯用的方法是使用 read-string

(process-grades
    (read-string weights)
    (read-string grades))
于 2014-02-22T13:37:55.647 回答