Skip to content

Instantly share code, notes, and snippets.

Created Feb 26, 2019
What would you like to do?
Rule of five experiment
(ns fives.core
(defn make-collection [size max]
(for [n (range size)]
(rand-int max)))
(defn square [n]
(* n n))
(defn make-flat-coll [] (->> (make-collection 100 1000)))
(defn make-square-coll [] (->> (make-collection 100 1000)
(map square)))
(defn make-lumpy-coll "make a collection with humps of outliers"[]
(let [bigset (->> (make-collection 1000 10000)
(map square)
bottom-hump (take 10 bigset)
middle-hump (->> (drop 400 bigset)
(take 50))
top-hump (take 300 (reverse bigset))]
(flatten (conj bottom-hump middle-hump top-hump))))
(defn find-median [coll]
(let [median-index (int (/ (count coll) 2))]
(nth (sort coll) median-index)))
(defn rand-5 [coll]
(->> coll
(take 5)))
(defn rule-5-compliant? "test if the median is between the min and max samples"
[coll sample]
(let [sample-min (reduce min sample)
sample-max (reduce max sample)
coll-median (find-median coll)]
(< sample-min coll-median sample-max)))
(defn percent-compliant "test 100 sets of samples against the collection"
(let [sample-sets (for [n (range 100)]
(rand-5 coll))]
(->> sample-sets
(map #(rule-5-compliant? coll %))
(filter true?)
(defn iterated-tests "test 1000 collections and return the stats"
(let [test-results (for [n (range 1000)]
(percent-compliant (function-maker)))]
{:min (reduce min test-results)
:max (reduce max test-results)
:average (int (/ (reduce + test-results) 1000))}))
(defn test-all-colls "tests all off the collection makers and returns their stats" []
{:flat (iterated-tests make-flat-coll)
:squared (iterated-tests make-square-coll)
:lumpy (iterated-tests make-lumpy-coll)})
;; Sample results:
;; {:flat {:min 85, :max 100, :average 93},
;; :squared {:min 85, :max 100, :average 93},
;; :lumpy {:min 86, :max 100, :average 93}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment