Created
November 30, 2016 10:10
-
-
Save jasonneylon/12fa59b83d2cebdca0bf4cd74352d4f1 to your computer and use it in GitHub Desktop.
Generate passphrases (and DNA!) using clojure.spec
This file contains hidden or 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
(require '[clojure.spec :as s]) | |
(require '[clojure.spec.gen :as gen]) | |
;; PASSPHRASES - generate a random 5 word passphrase | |
;; naive approach was to generate a random string and contrain to match a five word pattern | |
;; however this hit 100 generation limit imposed by clojure.spec | |
(defn words [str] (clojure.string/split str #" ")) | |
(def five-words? (comp (partial = 5) count words)) | |
(gen/generate (s/gen (s/and string? five-words?))) | |
;; Our second attempt was to read the Linux word dictionary into a set | |
;; and generate a five word list from it | |
(def words | |
(-> (slurp "/usr/share/dict/words") | |
(clojure.string/split #"\n") | |
(->> (filter #(= 4 (count %)))))) | |
(s/def ::word (s/and (set words))) | |
(s/def ::pass-phrase (s/every ::word :count 5)) | |
(gen/generate (s/gen ::pass-phrase)) | |
;; DNA | |
(s/def ::dna (s/every #{\G \T \C \A} :min-count 1)) | |
(gen/generate (s/gen ::dna)) | |
(s/exercise ::dna) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment