clojure.spec

Specification is much more powerful than static typing plus you also get validation, instrumentation and generative testing – clojure.spec (by Rich Hickey)

Example (spec for URIs)

 1(ns example.specs
 2  (:require #?(:clj  [clojure.spec :as s]
 3               :cljs [cljs.spec :as s])
 4            [clojure.string :as str]))
 5;; URI spec.
 6(def uri?
 7  (s/and string? #?(:clj #(try (do (java.net.URI. %) true) (catch Exception e false))
 8                    :cljs #(= (str/replace (js/encodeURI %) "%25" "%") %))))
 9;; e.g. Call as follows:
10(s/valid? uri? "https://www.google.nl/url?q=https://www.reddit.com/r/Clojure/comments/4kutl7/clojurespec_guide/&sa=U&ved=0ahUKEwic2v3-r6DQAhVBGsAKHcrVCZMQFggUMAA&usg=AFQjCNHs0DmF1uNIw9BYUK7pqpgp5HEbow")

Videos

Posts in this Series