Skip to content

Instantly share code, notes, and snippets.

@dandorman
Last active March 5, 2019 15:56
Show Gist options
  • Save dandorman/e1698fbe259c3fb706a6dda64cacfa6d to your computer and use it in GitHub Desktop.
Save dandorman/e1698fbe259c3fb706a6dda64cacfa6d to your computer and use it in GitHub Desktop.
clj -Sdeps '{:deps {hash-algo {:git/url "https://gist.github.com/dandorman/e1698fbe259c3fb706a6dda64cacfa6d" :sha "1fecec9e37629cefd6e826f724c821d550e3720e"}}}' -m hash-algo
{:paths ["."]
:deps {metasoarous/oz {:mvn/version "1.5.6"}}}
(ns hash-algo
(:require [oz.core :as oz]))
(defn prime? [n]
(or (= 2 n)
(not (some #(zero? (mod n %)) (range 2 (inc (Math/sqrt n)))))))
(defn primes []
(filter prime? (drop 2 (range))))
(def letters (->> (int \a)
(iterate inc)
(map char)
(take 26)
(into [])))
(def letter->prime (zipmap letters (primes)))
(defn rand-word []
(apply str (repeatedly (+ 5 (rand-int 6)) #(first (shuffle letters)))))
(defn word-score [word]
(let [score (->> word
(map letter->prime)
(reduce +))]
(mod score 10)))
(defn -main []
(oz/start-plot-server!)
(Thread/sleep 1000)
(let [words (repeatedly 10000 rand-word)
values (map (fn [w] {:word w :slot (word-score w)
:len (str (count w))})
words)]
(oz/v! {:data {:values values}
:encoding {:x {:field "slot"}
:y {:aggregate "count"
:field "len"
:type "nominal"}
:color {:field "len"
:type "nominal"}}
:mark "bar"})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment