Re: Basic string modification question

2014-04-03 Thread Andy Smith
Thanks Josef, I shall digest that reply very closely. I had actually initially tried assoc on the string as you described. (You guys are great. This is why Im really enjoying learning clojure.) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To pos

Re: Basic string modification question

2014-04-03 Thread Jozef Wagner
It all boils down to the fact that strings are immutable and are not persistent. What I was trying to say is that the operation of replacing one character with another in an immutable and non persistent string is not a common one and may be a sign of bad design. I was not commenting on the replace

Re: Basic string modification question

2014-04-03 Thread Softaddicts
Concatenation, replacing occurrences by another value, formatting values within a string are quite common. Playing with indexes within a string ? I rarely used this in the last 15 years. In the old days yes since space was at premium, many languages did not support dynamic allocations, speed was

Re: Basic string modification question

2014-04-03 Thread Andy Smith
Thanks Josef, but could you explain a bit more. Are you saying that the operation of replacing a substring (by index & length) with another substring is not a common operation? i.e. (defn replace-substring [s r start len] (str (subs s 0 start) r (subs s (+ len start If so, that does supr

Re: Basic string modification question

2014-04-02 Thread Jozef Wagner
No. IMO this is not a common operation and should not be in core. If you need do it a lot, you should not use java string in the first place. What should be included in core is a transient string, with support for assoc!. Jozef On Wed, Apr 2, 2014 at 7:49 PM, Andy Smith wrote: > just a shame i

Re: Basic string modification question

2014-04-02 Thread Andy Smith
just a shame its not in the core, no? On Wednesday, 2 April 2014 18:06:03 UTC+1, A. Webb wrote: > > Using subs (no need for join) is the way I would go, just define > > > (defn replace-at [s n c] (str (subs s 0 n) c (subs s (inc n > > (replace-at "hello" 1 "a") ;=> "hallo" > > and ca

Re: Basic string modification question

2014-04-02 Thread A. Webb
Using subs (no need for join) is the way I would go, just define (defn replace-at [s n c] (str (subs s 0 n) c (subs s (inc n (replace-at "hello" 1 "a") ;=> "hallo" and carry on. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To po

Re: Basic string modification question

2014-04-02 Thread Andy Smith
I guess my point is that if the java function is so good, then why doesnt the clojure library thinly wrap it, so that your code remains portable clojure? On Wednesday, 2 April 2014 12:50:00 UTC+1, guns wrote: > > On Wed 2 Apr 2014 at 04:06:40AM -0700, Andy Smith wrote: > > > If there is nothi

Re: Basic string modification question

2014-04-02 Thread guns
On Wed 2 Apr 2014 at 04:06:40AM -0700, Andy Smith wrote: > If there is nothing better then I wonder why there isn't something > like this in the clojure standard libraries (must be a good reason I > suppose)? Its a fairly standard function for a string library isnt it? It would be a terrible fun

Re: Basic string modification question

2014-04-02 Thread Max Penet
about 1. if that's meant to be portable, yes, not the best indeed, but if you look at c.c.string/* or even c.c/str, most of the functions use stringbuilder I think. about 2. probably slower than subs, these are just alternatives to what you suggested On Wednesday, April 2, 2014 1:06:40 PM UTC+2

Re: Basic string modification question

2014-04-02 Thread Andy Smith
the first isnt pure clojure wo I would probably try to avoid this... e.g. what if I want to port to clojureCLR? The second 'looks' quite a roundabout way of simply manipulating a string? How would the following compare for performance? (defn replace-substring [s r start len] (str (subs s 0 star

Re: Basic string modification question

2014-04-02 Thread Max Penet
There are many ways to do this, these 2 come to mind: (doto (StringBuilder. "abc") (.setCharAt 2 \d)) (apply str (assoc (vec "abc") 2 "d")) The former is probably a lot faster (uglier too)... On Wednesday, April 2, 2014 12:10:48 PM UTC+2, Andy Smith wrote: > > Hi, > > I see there are a lot of

Basic string modification question

2014-04-02 Thread Andy Smith
Hi, I see there are a lot of functions strings, but nothing that allows me to create a new string with a character replaced at a given index? Am I meant to use subs and join to do this? It seems a bit long-winded? I wonder why there isnt a helper function out of the box to do this (or is there?