Skip to content

Instantly share code, notes, and snippets.

@pcwerk
Created July 13, 2016 02:09
Show Gist options
  • Save pcwerk/a6563d53c95ce31f43dec2f068b170ca to your computer and use it in GitHub Desktop.
Save pcwerk/a6563d53c95ce31f43dec2f068b170ca to your computer and use it in GitHub Desktop.
Good way to get a tag a string and a map
(ns tag-map.clj
(:gen-class))
(defn tag
[word]
(str "`" word "`"))
(def line-str "This is a test to see if this works. It's a cool dog test. One must work hard to get the right answer.")
(def keywords
{"right" (tag "right")
"test" (tag "test")
"answer" (tag "answer")})
(defn upper
[line]
(clojure.string/upper-case line))
(defn upper-map [m f]
(into {} (for [[k v] m] [(f k) (f v)])))
(defn replace-map
"given an input string and a hash-map, returns a new string with all
keys in map found in input replaced with the value of the key"
[s1 m1]
(let [s (upper s1)
m (upper-map m1 clojure.string/upper-case)]
(clojure.string/replace s (re-pattern (apply str (interpose "|" (map #(java.util.regex.Pattern/quote %) (keys m))))) m)))
(defn -main
[& args]
(println (replace-map line-str keywords)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment