Skip to content

Instantly share code, notes, and snippets.

@jreighley
Created February 26, 2019 08:20
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 jreighley/9b0309bee4df55f87381286060a2ddfc to your computer and use it in GitHub Desktop.
Save jreighley/9b0309bee4df55f87381286060a2ddfc to your computer and use it in GitHub Desktop.
Rule of five experiment
(ns fives.core
(:gen-class))
(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)
(sort))
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
(shuffle)
(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"
[coll]
(let [sample-sets (for [n (range 100)]
(rand-5 coll))]
(->> sample-sets
(map #(rule-5-compliant? coll %))
(filter true?)
(count))))
(defn iterated-tests "test 1000 collections and return the stats"
[function-maker]
(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