Created
August 17, 2014 18:24
-
-
Save maxcountryman/5212cbe1177f0b399897 to your computer and use it in GitHub Desktop.
This file contains 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
(def ^{:const true :private true} | |
base-62-alphabet | |
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") | |
(defn encode | |
"Encodes v in a new base of ks." | |
[ks v] | |
(let [base (count ks)] | |
(apply str | |
(reduce | |
(fn [acc v] | |
(conj acc (nth ks (mod v base)))) | |
nil | |
(take-while pos? (iterate #(quot % base) v)))))) | |
(def ^{:doc "Encodes a given value into a base62 representation."} | |
encode-base-62 | |
(partial encode base-62-alphabet)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment