SHARE
TWEET

Untitled

a guest Nov 17th, 2015 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns for-repl.core-typed-examples
  2.   (:require [clojure.core.typed :refer [ann
  3.                                         ann-record
  4.                                         ann-datatype
  5.                                         ann-form
  6.                                         defalias
  7.                                         HMap
  8.                                         Map
  9.                                         Any
  10.                                         All
  11.                                         U
  12.                                         Seq
  13.                                         Keyword
  14.                                         Fn]]
  15.             [clojure.core.match :refer [match]]))
  16.  
  17. (defalias Failure (All [a] (HMap :mandatory {:exception a} :complete? true)))
  18.  
  19. (defalias Success (All [a] (HMap :mandatory {:result a} :complete? true)))
  20.  
  21. (ann as-success (All [a] [a -> Success]))
  22. (defn ^:no-check as-success [value]
  23.   (ann-form {:result value} Success))
  24.  
  25. (ann as-failure (All [a] [a -> Failure]))
  26. (defn ^:no-check as-failure [exception]
  27.   (ann-form {:exception exception} Failure))
  28.  
  29. (defalias Try (U Failure Success))
  30.  
  31. (ann parse-long [String -> Try])
  32. (defn parse-long [str]
  33.   (try
  34.     (as-success (Long/parseLong str))
  35.     (catch NumberFormatException e
  36.       (as-failure e))))
  37.  
  38. (ann-record Human [name :- String weight :- Long])
  39. (defrecord Human [name weight])
  40.  
  41. (ann human [String Long -> Human])
  42. (defn human [name weight]
  43.   (Human. name weight))
  44.  
  45. (defalias Args (U (Seq String) nil))
  46.  
  47. (ann args->human [Args -> Try])
  48. (defn ^:no-check args->human [args]
  49.   (as-> {:args "Wrong number of arguments"
  50.          :name-presence "Name there must be"
  51.          :weight-presence "Weight there must be"
  52.          :weight-format "Weight must be Long"}
  53.       errors
  54.     (match (vec args)
  55.            [name weight]
  56.            (match [(parse-long weight)]
  57.                   [^Try {:result v}] (as-success (human name v))
  58.                   [^Try {:exception _}] (as-failure (select-keys errors [:weight-format])))
  59.            [] (as-failure (select-keys errors [:args :name-presence :weight-presence]))
  60.            [_] (as-failure (select-keys errors [:args :weight-presence]))
  61.            :else (as-failure (select-keys errors [:args])))))
  62.  
  63. (defalias ExData (U nil (Map Any Any)))
  64.  
  65. (ann log-error [ExData -> nil])
  66. (defn log-error [errors]
  67.   (some-> errors println))
  68.  
  69. (ann print-human [Human -> nil])
  70. (defn print-human [human]
  71.   (println human))
  72.  
  73. (ann check [String * -> nil])
  74. (defn check [& args]
  75.   (match [(args->human args)]
  76.          [^Try {:result v}] (print-human v)
  77.          [^Try {:exception e}] (-> e println)))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top