Created
April 17, 2012 17:19
-
-
Save matthewp/2407601 to your computer and use it in GitHub Desktop.
Soundex algorithm in Clojure
This file contains hidden or 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 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