Last active
August 16, 2016 15:03
-
-
Save burbma/e2e33cb526ed8c25549edbdb6e48f711 to your computer and use it in GitHub Desktop.
clojure spec seq of maps
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
(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