Re: Multiple replacements in string using a map

2013-04-24 Thread shinmuro
if map is following: {:_ACCT-ID_ 9876 :_ACCT-TYP_ "B"} then: (defn sql-map-replace [sql m] (letfn [(quote-if-str [s] (if (string? s) (str "'" s "'") s))] (reduce-kv (fn [ret k v] (clojure.string/replace ret (re-pattern (str "(.*)" (name k) "(.*)"

Re: Multiple replacements in string using a map

2013-04-23 Thread mond
Chapeau - nicely, ahem, reduced On Tuesday, April 23, 2013 8:23:27 AM UTC+2, rod naph wrote: > > I scratched my itch with... > > > (defn map-replace [m text] > (reduce > (fn [acc [k v]] (s/replace acc (str k) (str v))) > text m)) > > > Hope it helps. > > On Tuesday, April 23, 2013 5:55:5

Re: Multiple replacements in string using a map

2013-04-22 Thread rod naph
I scratched my itch with... (defn map-replace [m text] (reduce (fn [acc [k v]] (s/replace acc (str k) (str v))) text m)) Hope it helps. On Tuesday, April 23, 2013 5:55:51 AM UTC+1, Mond Ray wrote: > > Man - you guys are good and *fast*. > > I was pootling around with a version that w

Re: Multiple replacements in string using a map

2013-04-22 Thread Mond Ray
Man - you guys are good and *fast*. I was pootling around with a version that would directly replace "/a/:key/b" {:key "value"} with "/a/value/b" and a failed version sneaked into my code. Incidentally that is my only slight complaint about the code as it stands: I have to use something like

Re: Multiple replacements in string using a map

2013-04-22 Thread Sean Corfield
On Mon, Apr 22, 2013 at 1:45 PM, Mond Ray wrote: > Something very odd going on here - one day it works the next day it fails :( This code is different to what you posted the other day... > #_=> (map #(java.util.regex.Pattern/quote (keyword %))) That won't work - Pattern/quote will not acc

Re: Multiple replacements in string using a map

2013-04-22 Thread Andy Fingerhut
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 wrote: > Something very odd going on here - one day it works the

Re: Multiple replacements in string using a map

2013-04-22 Thread Mond Ray
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

Re: Multiple replacements in string using a map

2013-04-20 Thread Mond Ray
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 copie

Re: Multiple replacements in string using a map

2013-04-19 Thread Andy Fingerhut
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 yo

Re: Multiple replacements in string using a map

2013-04-19 Thread Sean Corfield
I just tried the code in a fresh REPL with Clojure 1.5.1 and it works, so I tried it with Clojure 1.4.0 and it works. Well, assuming you do this first: (require '[clojure.string :as s]) What version of Clojure are you using? Are you doing the require? Do you have something else defined as `s`? Se

Re: Multiple replacements in string using a map

2013-04-19 Thread Mond Ray
Old thread but what the heck... it doesn't work in my REPL user=> (defn key-pattern #_=> "Create a regex Pattern of the form '|', the key names #_=> will be quoted in case they contain special regex characters" #_=> [m] #_=> (->> (keys m) #_=> (map #(java.util.regex.

Re: Multiple replacements in string using a map

2011-03-15 Thread Aaron Cohen
On Mon, Mar 14, 2011 at 2:17 PM, Daniel Solano Gomez 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 safet

Re: Multiple replacements in string using a map

2011-03-14 Thread semperos
There is also this contrib code which facilitates string interpolation: http://clojure.github.com/clojure-contrib/strint-api.html But I second Ken's concern about SQL injection attacks. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this

Re: Multiple replacements in string using a map

2011-03-14 Thread Ken Wesson
On Mon, Mar 14, 2011 at 2:17 PM, Daniel Solano Gomez 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_" >> >> I have a map such as: >> >> {:_ACCT-ID_ 9876 :_ACCT-TYP "B"} >> >> I wa

Re: Multiple replacements in string using a map

2011-03-14 Thread Daniel Solano Gomez
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_" > > I have a map such as: > > {:_ACCT-ID_ 9876 :_ACCT-TYP "B"} > > I want to write a clojure function that will take the string and map as