Last active
March 5, 2019 15:56
-
-
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
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
{:paths ["."] | |
:deps {metasoarous/oz {:mvn/version "1.5.6"}}} |
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
(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