Skip to content

Instantly share code, notes, and snippets.

@matthewp
Created April 17, 2012 17:19
Show Gist options
  • Save matthewp/2407601 to your computer and use it in GitHub Desktop.
Save matthewp/2407601 to your computer and use it in GitHub Desktop.
Soundex algorithm in Clojure
(defn soundex [data]
(let [ contains-char? (fn [the-string the-char]
(some #(= the-char %) the-string))
find-first-map-val (fn [m v]
(last (first (filter #(contains-char? (first %) v) m))))
compress (fn [xs]
(reduce #(if (= (last %1) %2) %1 (concat %1 (list %2))) '() xs))
fill (fn [x l w]
(if (> l (.length (str x))) (recur (str x w) l w) (.substring (str x) 0 l)))
code-gen { "BFPV" "1"
"CGJKQSXZ" "2"
"DT" "3"
"L" "4"
"MN" "5"
"R" "6" }
data (clojure.string/upper-case (first (clojure.string/split data #"\s")))
result (map (fn [x]
(find-first-map-val code-gen x)) (rest data)) ]
(fill (reduce str (compress (keep identity (cons (first data) result))))
4 "0")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment