Advertisement
Guest User

Untitled

a guest
Nov 10th, 2015
160
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Clojure 1.34 KB | None
  1. (ns for-repl.core
  2.   (:require [clojure.core.typed :refer [defn ann ann-record let cf doseq HMap Any U Seq]]))
  3.  
  4. (ann ^:no-check clojure.core/assoc! [Any Any Any Any * -> nil])
  5. (ann ^:no-check clojure.core/transient [(HMap :complete? true) -> Any])
  6. (ann ^:no-check clojure.core/persistent! [Any -> (HMap :optional {:name String :weight String})])
  7.  
  8. (ann-record Human [name :- String weight :- Long])
  9. (defrecord Human [name weight])
  10.  
  11. (defn human [name :- String weight :- Long] :- Human
  12.   (Human. name weight))
  13.  
  14. (defn validate-human-args [name :- (U String nil) weight :- (U String nil)] :- (HMap :optional {:name String :weight String} :complete? true)
  15.   (let [errors (transient {})]
  16.     (when-not name
  17.       (assoc! errors :name "Name there must be"))
  18.     (when-not weight
  19.       (assoc! errors :weight "Weight there must be"))
  20.     (persistent! errors)))
  21.  
  22. (defn parse-long [str :- String] :- Long
  23.   (Long/parseLong str))
  24.  
  25. (defn -main [& args :- String *] :- nil
  26.   (let [[name weight] args
  27.         errors (validate-human-args name weight)]
  28.     (if (seq errors)
  29.       (doseq [[field :- Symbol message :- String] errors]
  30.         (println message))
  31.       (when (and name weight)
  32.         (try
  33.           (println (human name (parse-long weight)))
  34.           (catch NumberFormatException e
  35.             (println "Weight must be integer!")))))))
Advertisement
RAW Paste Data Copied
Advertisement