Skip to content

Instantly share code, notes, and snippets.

@mharju
Last active Jun 6, 2016
Embed
What would you like to do?
; 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