Skip to content

Instantly share code, notes, and snippets.

@jclaggett
Last active December 15, 2016 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jclaggett/2446a199a6fb3c05250a0eb429d8fc79 to your computer and use it in GitHub Desktop.
Save jclaggett/2446a199a6fb3c05250a0eb429d8fc79 to your computer and use it in GitHub Desktop.
(ns squarespec
(:require [clojure.spec :as s]
[clojure.spec.gen :as sgen]
[clojure.spec.test :as stest]))
;; Implementation
(defn square
"Define a square path given an origin and length."
[[x1 y1 :as origin] length]
(let [x2 (+ x1 length)
y2 (+ y1 length)]
[origin [x2 y1] [x2 y2] [x1 y2]]))
;; Specification
(s/def ::scalar
(s/or :int int?
:double (s/double-in :NaN? false :infinite? false)))
(s/def ::point
(s/coll-of ::scalar :kind sequential? :count 2))
(s/def ::points
(s/coll-of ::point :kind sequential?))
(s/fdef square
:args (s/cat :origin ::point :length ::scalar)
:ret ::points
:fn (fn [{[x1 y1 :as origin] :args
ret :ret}]
(and (= origin (second ret))
(= 4 (count ret)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment