Last active
November 25, 2016 00:06
-
-
Save triss/1128bcfeb1a71c7e09ffd9cf10be0370 to your computer and use it in GitHub Desktop.
Getting ranges from specs...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Specs for matching | |
(defn- fn-wrapped-spec | |
[spec] | |
(s/spec (s/cat :fn #{'clojure.core/fn} | |
:args #{['%]} | |
:form (s/spec spec)))) | |
(s/def ::int-in-range?-form | |
(s/cat :fn #{'clojure.spec/int-in-range?} | |
:min number? | |
:max number? | |
:x #{'%})) | |
(s/def ::int-in-form | |
(s/cat :and #{'clojure.spec/and} | |
:int? #{'clojure.core/int?} | |
:fn (fn-wrapped-spec ::int-in-range?-form))) | |
(s/def ::double-in-form | |
(s/cat :and #{'clojure.spec/and} | |
:double? #{'clojure.core/double?} | |
:max (fn-wrapped-spec (s/cat :<= #{'clojure.core/<=} | |
:x #{'%} | |
:max number?)) | |
:min (fn-wrapped-spec (s/cat :<= #{'clojure.core/<=} | |
:min number? | |
:x #{'%})))) | |
(s/def ::range-spec-form | |
(s/or :double-in ::double-in-form | |
:int-in ::int-in-form)) | |
;; Fns for extractining min/maximum | |
(defn double-in->min-max | |
[c] {:min (get-in c [:min :form :min]) | |
:max (get-in c [:max :form :max])}) | |
(defn int-in->min-max | |
[c] {:min (get-in c [:fn :form :min]) | |
:max (get-in c [:fn :form :max])}) | |
(defn form->min-max | |
[form] | |
(let [[t c] (s/conform ::range-spec-form form)] | |
(condp = t | |
:int-in (int-in->min-max c) | |
:double-in (double-in->min-max c)))) | |
(def spec->min-max | |
(comp form->range s/form)) | |
(comment | |
(s/def ::int-range (s/int-in 0 100)) | |
(s/def ::double-range (s/double-in :min 0 :max 5.1)) | |
(spec->min-max ::int-range) | |
(spec->min-max ::double-range)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment