Created
September 14, 2016 14:53
-
-
Save ajchemist/3a03e084c70bcf0ad455f299f45ae161 to your computer and use it in GitHub Desktop.
namespace string shortener
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
(defn ns-str-shortener | |
([ns-str] ns-str) | |
([ns-str number] | |
(let [length (.length ns-str)] | |
(if (< number length) | |
(let [splitted (.split #"\." ns-str) | |
endi (dec (alength splitted)) | |
short (volatile! false)] | |
(areduce splitted i ret "" | |
(let [j (- endi i) | |
comp (aget splitted j) | |
length (+ (.length ret) (.length comp) 1) | |
comp (cond | |
@short (subs comp 0 1) | |
(== i 0) comp | |
(< number length) (do (vreset! short true) | |
(subs comp 0 1)) | |
:else comp)] | |
(str (when-not (== j 0) ".") comp ret)))) | |
ns-str)))) | |
(ns-str-shortener "namespace.somewhat.long.example" 16) | |
;; => "n.s.long.example" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment