You changed the definition of key-pattern. The original had "name" where your most recent version has "keyword". Change it back to "name" and at least I was able to get it to work.
Andy On Mon, Apr 22, 2013 at 1:45 PM, Mond Ray <mondraym...@gmail.com> wrote: > Something very odd going on here - one day it works the next day it fails > :( > > $ lein repl > nREPL server started on port 51502 > REPL-y 0.1.10 > Clojure 1.5.1 > Exit: Control+D or (exit) or (quit) > Commands: (user/help) > Docs: (doc function-name-here) > (find-doc "part-of-name-here") > Source: (source function-name-here) > (user/sourcery function-name-here) > Javadoc: (javadoc java-object-or-class-here) > Examples from clojuredocs.org: [clojuredocs or cdoc] > (user/clojuredocs name-here) > (user/clojuredocs "ns-here" "name-here") > user=> (require '[clojure.string :as s]) > nil > user=> ; code from the clojure google group to help map properties into > paths > > user=> ; first a way to take the keys and produce a regex > > user=> (defn key-pattern > #_=> "Create a regex Pattern of the form '<key1>|<key2>', the key names > #_=> will be quoted in case they contain special regex characters" > #_=> [m] > #_=> (->> (keys m) > #_=> (map #(java.util.regex.Pattern/quote (keyword %))) > #_=> (s/join "|") > #_=> java.util.regex.Pattern/compile)) > #'user/key-pattern > user=> > > user=> ; second a way to use the above function to achieve the replacement > > user=> (defn replace-map [text m] > #_=> "Replace keys in a String from matching values in a map" > #_=> (s/replace text > #_=> (key-pattern m) > #_=> (fn [field-name] > #_=> (java.util.regex.Matcher/quoteReplacement (str (get m > (keyword field-name))))))) > #'user/replace-map > user=> > > user=> (def param-map {:$tld "com"}) > #'user/param-map > user=> > > user=> (def url (replace-map "https://google.$tld" param-map)) > ClassCastException clojure.lang.Keyword cannot be cast to java.lang.String > user/key-pattern/fn--352 (NO_SOURCE_FILE:6) > > user=> > > You see my full REPL session. > > I reckon I must be doing something dumb wrong - any ideas on what's going > on? > > Thanks > > Ray > > On Saturday, 20 April 2013 09:51:21 UTC+2, Mond Ray wrote: >> >> Just tried again - using lein repl (clojure 1.4.0) and it worked fine. >> >> It was late - who knows what I did ;-) >> >> Thanks for checking guys. >> >> On Saturday, 20 April 2013 02:37:14 UTC+2, Andy Fingerhut wrote: >>> >>> I fired up a Clojure 1.5.1 REPL, did (require '[clojure.string :as s]) >>> first, then copied and pasted those two function definitions, and did not >>> get the errors you are seeing. I don't have a good guess why you are >>> getting those errors. Did you do the require first? What version of >>> Clojure are you using? >>> >>> Andy >>> >>> >>> On Fri, Apr 19, 2013 at 5:17 PM, Mond Ray <mondr...@gmail.com> wrote: >>> >>>> Old thread but what the heck... it doesn't work in my REPL >>>> >>>> user=> (defn key-pattern >>>> #_=> "Create a regex Pattern of the form '<key1>|<key2>', the key >>>> names >>>> #_=> will be quoted in case they contain special regex characters" >>>> #_=> [m] >>>> #_=> (->> (keys m) >>>> #_=> (map #(java.util.regex.Pattern/**quote (name %))) >>>> #_=> (s/join "|") >>>> #_=> java.util.regex.Pattern/**compile)) >>>> #'user/key-pattern >>>> user=> >>>> >>>> user=> (defn replace-map [text m] >>>> #_=> (s/replace text >>>> #_=> (key-pattern m) >>>> #_=> (fn [field-name] >>>> #_=> (java.util.regex.Matcher/**quoteReplacement (str (get >>>> m >>>> #_=> (keyword field-name))))))) >>>> #'user/replace-map >>>> user=> (replace-map "/path/:p0/b/:p1" {:p0 "1" :p1 "2"}) >>>> ClassCastException java.lang.String cannot be cast to clojure.lang.IFn >>>> user/key-pattern/fn--408 (NO_SOURCE_FILE:6) >>>> >>>> user=> (key-pattern {:a 1}) >>>> ClassCastException java.lang.String cannot be cast to clojure.lang.IFn >>>> user/key-pattern/fn--408 (NO_SOURCE_FILE:6) >>>> >>>> Am I doing something wrong or is there a typo in your code? >>>> >>>> >>>> >>>> On Tuesday, 15 March 2011 16:35:04 UTC+1, Aaron Cohen wrote: >>>> >>>>> On Mon, Mar 14, 2011 at 2:17 PM, Daniel Solano Gomez >>>>> <clo...@sattvik.com> wrote: >>>>> > On Mon Mar 14 13:02 2011, shuaybi2 shuaybi2 wrote: >>>>> >> I have a string such as: >>>>> >> >>>>> >> "select * from account where acctId = _ACCT-ID_ and acctTyp = >>>>> _ACCT-TYP_" >>>>> >>>>> There are several clojure libraries that exist to improve the ease and >>>>> safety of doing something like this. Amongst them are >>>>> clojure.contrib.sql and ClojureQL, which take different approaches. >>>>> They all should be sufficient to guard against SQL injection and >>>>> should probably be the first place you look. >>>>> >>>>> For the more general question you were asking about how to generically >>>>> replace a map of matches-to-replacements though, Daniel did a good job >>>>> showing how to use a reduce over the map. That method will call >>>>> "replaceAll" once per entry in the map, which is probably fine if you >>>>> don't have many substitutions. >>>>> >>>>> Another way to do it is using clojure.string.replace, which has an >>>>> often-overlooked third overload which matches with a regex and >>>>> replaces with a "mapping function." >>>>> >>>>> Starting with a simple example: >>>>> user=>(require '[clojure.string :as s]) >>>>> nil >>>>> user=>(s/replace "a b a" #"a|b" {"a" "1" "b" "2"}) >>>>> "1 2 1" >>>>> >>>>> In the example, the map was being used as a "replacement function". >>>>> >>>>> --- >>>>> If you're willing to change your map to use strings as keys and >>>>> values, then the previous example is good enough. >>>>> >>>>> Otherwise, because you're wanting to use keywords as your keys, and >>>>> arbitratry values for your values, we'll need to use a slightly more >>>>> sophisticated replacement function. >>>>> >>>>> (defn key-pattern >>>>> "Create a regex Pattern of the form '<key1>|<key2>', the key names >>>>> will be quoted in case they contain special regex characters" >>>>> [m] >>>>> (->> (keys m) >>>>> (map #(java.util.regex.Pattern/**quot**e (name %))) >>>>> (s/join "|") >>>>> java.util.regex.Pattern/**compil**e)) >>>>> >>>>> (defn replace-map [text m] >>>>> (s/replace text >>>>> (key-pattern m) >>>>> (fn [field-name] >>>>> (java.util.regex.Matcher/**quote**Replacement (str (get m >>>>> (keyword field-name))))))) >>>>> >>>>> -- >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Clojure" group. >>>> To post to this group, send email to clo...@googlegroups.com >>>> Note that posts from new members are moderated - please be patient with >>>> your first post. >>>> To unsubscribe from this group, send email to >>>> clojure+u...@googlegroups.com >>>> For more options, visit this group at >>>> http://groups.google.com/**group/clojure?hl=en<http://groups.google.com/group/clojure?hl=en> >>>> --- >>>> You received this message because you are subscribed to the Google >>>> Groups "Clojure" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to clojure+u...@googlegroups.com. >>>> For more options, visit >>>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out> >>>> . >>>> >>>> >>>> >>> >>> -- > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.