zelark / nano-id

A unique string ID generator for Clojure and ClojureScript (・_・)ノ
MIT License
191 stars 9 forks source link

[custom.generate] building string by adding characters to beginning of line #1

Closed zelark closed 6 years ago

zelark commented 6 years ago

A goal is generating IDs in lexicographical order. And there are 2 ways to reach for the goal: 1) To do like topic says; 2) Producing numbers in example below in reverse order:

(let [alphabet "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~"
      time-gen (fn [n] (take n (iterate #(bit-shift-right % 6)
                                        (quot (System/currentTimeMillis) 1000))))
      time-id  (generate alphabet time-gen)]
  (time-id 6))