Skip to content

Instantly share code, notes, and snippets.

@edbond
Created April 17, 2015 19:13
Show Gist options
  • Save edbond/16da5f6678aee5e1ca68 to your computer and use it in GitHub Desktop.
Save edbond/16da5f6678aee5e1ca68 to your computer and use it in GitHub Desktop.
int to "localized" string in ClojureScript
(def i 343224324234)
(defn int->str-1
[]
(let [s (str i) p (mod (count s) 3)]
(->>
(reduce-kv
(fn [acc i c]
(if (and (> i 0) (zero? (mod (- i p) 3)))
(conj acc \, c)
(conj acc c))) [] (vec s))
(apply str))))
(defn int->str-2
[]
(->> i
str
reverse
(partition 3 3 [])
(interpose ",")
flatten
reverse
(apply str)))
(prn (int->str-1))
(time
(dotimes [_ 10000]
(int->str-1)))
(prn (int->str-2))
(time
(dotimes [_ 10000]
(int->str-2)))
;; "343,224,324,234"
;; "Elapsed time: 355 msecs"
;; "343,224,324,234"
;; "Elapsed time: 3040 msecs"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment