Skip to content

Instantly share code, notes, and snippets.

@maxcountryman
Created August 17, 2014 18:24
Show Gist options
  • Save maxcountryman/5212cbe1177f0b399897 to your computer and use it in GitHub Desktop.
Save maxcountryman/5212cbe1177f0b399897 to your computer and use it in GitHub Desktop.
(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