Skip to content

Instantly share code, notes, and snippets.

@sanatgersappa
Created November 28, 2015 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanatgersappa/3d472675bbd06ffa84a1 to your computer and use it in GitHub Desktop.
Save sanatgersappa/3d472675bbd06ffa84a1 to your computer and use it in GitHub Desktop.
(defn get-digit [n]
(cond
(nil? n) "_"
(nil? (re-find #"\d" (str n))) ""
:else n))
(defn get-char [n]
(cond
(nil? n) "_"
(nil? (re-find #"[A-Za-z]" (str n))) ""
:else n))
(defn apply-mask [mask input]
(loop [filteredmask mask i input output ""]
(let [m (first filteredmask)]
(cond
(nil? m) (if (= "" output) i output)
(= "#" (str m)) (recur (rest filteredmask) (rest i) (str output (get-digit (first i))))
(= "x" (str m)) (recur (rest filteredmask) (rest i) (str output (get-char (first i))))
:else (recur (rest filteredmask) i (str output m))))))
(apply-mask "(###) ### ####" "6666666666")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment