Skip to content

Instantly share code, notes, and snippets.

@mharju
Last active June 6, 2016 16:52
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 mharju/c175978941487e117c64e4a55ea83529 to your computer and use it in GitHub Desktop.
Save mharju/c175978941487e117c64e4a55ea83529 to your computer and use it in GitHub Desktop.
; How to accomplish this so that the generator works as well?
(ns voting.core
(:require [clojure.spec :as s]
[clojure.spec.gen :as gen]))
(defn valid-card? [s]
(-> (loop [n 0 s (sort s)]
(if (empty? s) n
(when (= (inc n) (first s)) (recur (inc n) (next s)))))
(integer?)))
(s/def ::card (s/and
;; Must contain a list of positive integers
(s/+ (s/and integer? #(> % 0)))
;; That is a permutation of the numbers from 1 to (count card)
valid-card?))
; true
(s/valid? ::card [3 2 1 4])
; false
(s/valid? ::card [3 2 1 5])
; ExceptionInfo Couldn't satisfy such-that predicate after 100 tries. clojure.core/ex-info (core.clj:4617)
(gen/sample (s/gen ::card))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment