Skip to content

Instantly share code, notes, and snippets.

@burbma
Last active August 16, 2016 15:03
Show Gist options
  • Save burbma/e2e33cb526ed8c25549edbdb6e48f711 to your computer and use it in GitHub Desktop.
Save burbma/e2e33cb526ed8c25549edbdb6e48f711 to your computer and use it in GitHub Desktop.
clojure spec seq of maps
(ns unicorn
(:require [#?(:clj clojure.spec :cljs cljs.spec) :as s]
[#?(:clj clojure.spec.test :cljs cljs.spec.test) :as stest]))
(def names ["Eugene" "Smith" "Crabbs" "Jeff" "Shelly" "Barbra" "Joanne" "Jane"])
(s/def ::name (into #{} names))
(s/def ::is-white boolean?)
(s/def ::has-horn boolean?)
(s/def ::unicorn
(s/keys :req [::is-white ::has-horn]
:opt [::name]))
(def unicorns [{::name "Eugene"
::is-white true
::has-horn true}
{:is-white false
::has-horn false}])
(s/fdef name-unicorns
:args (s/cat :unicorns (s/coll-of ::unicorn)))
(defn name-unicorns
"(Re)name all unicorns"
[unicorns]
(map #(assoc % ::name (rand-nth names)) unicorns))
(stest/instrument `name-unicorns)
(name-unicorns unicorns)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment