0

先生の友達が成績を計算するのに役立つ簡単な Web アプリを作成しています。以下で作業している現在のコードがあります。

    (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"))}))))

私は、データが文字列として対応するシンボルにバインドされるweightsと推測しています。gradesただし、計算関数で float と vector を使用するには、これらの引用符を外す必要があります。これどうやってするの?私も初心者なので、コードに間違いや間違ったやり方があれば教えてください。また、より多くの名前空間または project.clj 情報が必要な場合は、尋ねてください。拡張します。

4

1 に答える 1

0

Java interop を使用して文字列を浮動小数点数または整数に変換できますが、慣用的な方法は read-string を使用することです

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