www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - more naming

reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
OK, so we have replace(haystack, needle, nail) which replaces _all_ 
occurrences of needle in haystack with nail. How would you call a 
function that replaces only the _first_ occurrence of needle with nail?

Must be a distinct function, not a runtime parameter to the existing 
function. This is because the function that replaces only one occurrence 
only requires nail to be an input range.


Andrei
Jan 22 2011
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
What if you want to replace a _count_ number of occurrences of needle
in haystack with nail? That's what Python's replace does, although I
think that only works for strings in Python.
Jan 22 2011
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 1/22/11 10:47 AM, Andrej Mitrovic wrote:
 What if you want to replace a _count_ number of occurrences of needle
 in haystack with nail? That's what Python's replace does, although I
 think that only works for strings in Python.
A specific count is rare but can be added as a defaulted parameter. The only problem is that one is special because it accepts a weaker range as replacement. Andrei
Jan 22 2011
prev sibling next sibling parent reply Justin Johansson <jj nospam.com> writes:
On 23/01/11 03:27, Andrei Alexandrescu wrote:
 OK, so we have replace(haystack, needle, nail) which replaces _all_
 occurrences of needle in haystack with nail. How would you call a
 function that replaces only the _first_ occurrence of needle with nail?

 Must be a distinct function, not a runtime parameter to the existing
 function. This is because the function that replaces only one occurrence
 only requires nail to be an input range.


 Andrei
If you articulate this question to yourself over and over until you understand the *salient* problem (i.e. the real problem that sticks out) you will answer your own question and then have no need to resort to newsgroup answers. If you are feeling lost, a good tactic to solve your problem is to think about function signatures in all their D glory not forgetting, of course, about D's innovative transitive xxx (const/immutable) constructs to include in the specification of your function(s). Justin
Jan 22 2011
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 1/22/11 11:03 AM, Justin Johansson wrote:
 On 23/01/11 03:27, Andrei Alexandrescu wrote:
 OK, so we have replace(haystack, needle, nail) which replaces _all_
 occurrences of needle in haystack with nail. How would you call a
 function that replaces only the _first_ occurrence of needle with nail?

 Must be a distinct function, not a runtime parameter to the existing
 function. This is because the function that replaces only one occurrence
 only requires nail to be an input range.


 Andrei
If you articulate this question to yourself over and over until you understand the *salient* problem (i.e. the real problem that sticks out) you will answer your own question and then have no need to resort to newsgroup answers. If you are feeling lost, a good tactic to solve your problem is to think about function signatures in all their D glory not forgetting, of course, about D's innovative transitive xxx (const/immutable) constructs to include in the specification of your function(s). Justin
Now I am feeling lost indeed :o). Andrei
Jan 22 2011
prev sibling next sibling parent reply Torarin <torarind gmail.com> writes:
2011/1/22 Andrei Alexandrescu <SeeWebsiteForEmail erdani.org>:
 OK, so we have replace(haystack, needle, nail) which replaces _all_
 occurrences of needle in haystack with nail. How would you call a function
 that replaces only the _first_ occurrence of needle with nail?

 Must be a distinct function, not a runtime parameter to the existing
 function. This is because the function that replaces only one occurrence
 only requires nail to be an input range.


 Andrei
It sounds like the current replace should be named replaceAll. Torarin
Jan 22 2011
next sibling parent so <so so.do> writes:
 It sounds like the current replace should be named replaceAll.

 Torarin
replaceAll replaceN replaceFirst replaceLast
Jan 22 2011
prev sibling next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 1/22/11 11:38 AM, Torarin wrote:
 2011/1/22 Andrei Alexandrescu<SeeWebsiteForEmail erdani.org>:
 OK, so we have replace(haystack, needle, nail) which replaces _all_
 occurrences of needle in haystack with nail. How would you call a function
 that replaces only the _first_ occurrence of needle with nail?

 Must be a distinct function, not a runtime parameter to the existing
 function. This is because the function that replaces only one occurrence
 only requires nail to be an input range.


 Andrei
It sounds like the current replace should be named replaceAll. Torarin
That's what I'm fearing - changing current replace to replaceAll and adding replace with the meaning of replaceFirst would silently change the semantics of existing code. Andrei
Jan 22 2011
next sibling parent reply Daniel Gibson <metalcaedes gmail.com> writes:
Am 22.01.2011 18:46, schrieb Andrei Alexandrescu:
 On 1/22/11 11:38 AM, Torarin wrote:
 2011/1/22 Andrei Alexandrescu<SeeWebsiteForEmail erdani.org>:
 OK, so we have replace(haystack, needle, nail) which replaces _all_
 occurrences of needle in haystack with nail. How would you call a
 function
 that replaces only the _first_ occurrence of needle with nail?

 Must be a distinct function, not a runtime parameter to the existing
 function. This is because the function that replaces only one occurrence
 only requires nail to be an input range.


 Andrei
It sounds like the current replace should be named replaceAll. Torarin
That's what I'm fearing - changing current replace to replaceAll and adding replace with the meaning of replaceFirst would silently change the semantics of existing code. Andrei
IMHO replace (without eny suffix) sounds like it replaces every occurence. So just add replaceFirst for a function that replaces only the first occurence :) Cheers, - Daniel
Jan 22 2011
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Daniel Gibson:

 IMHO replace (without eny suffix) sounds like it replaces every occurence.
 So just add replaceFirst for a function that replaces only the first 
 occurence :)
OK. Bye, bearophile
Jan 22 2011
prev sibling parent Paul D. Anderson <paul.removethis.d.anderson comcast.andthis.net> writes:
Daniel Gibson Wrote:

 Am 22.01.2011 18:46, schrieb Andrei Alexandrescu:
 On 1/22/11 11:38 AM, Torarin wrote:
 2011/1/22 Andrei Alexandrescu<SeeWebsiteForEmail erdani.org>:
 OK, so we have replace(haystack, needle, nail) which replaces _all_
 occurrences of needle in haystack with nail. How would you call a
 function
 that replaces only the _first_ occurrence of needle with nail?

 Must be a distinct function, not a runtime parameter to the existing
 function. This is because the function that replaces only one occurrence
 only requires nail to be an input range.


 Andrei
It sounds like the current replace should be named replaceAll. Torarin
That's what I'm fearing - changing current replace to replaceAll and adding replace with the meaning of replaceFirst would silently change the semantics of existing code. Andrei
IMHO replace (without eny suffix) sounds like it replaces every occurence. So just add replaceFirst for a function that replaces only the first occurence :) Cheers, - Daniel
This was my first thought, too. Paul
Jan 22 2011
prev sibling parent Torarin <torarind gmail.com> writes:
2011/1/22 Andrei Alexandrescu <SeeWebsiteForEmail erdani.org>:

 That's what I'm fearing - changing current replace to replaceAll and adding
 replace with the meaning of replaceFirst would silently change the semantics
 of existing code.

 Andrei
Yeah, I see that problem. I'm just so used to "replace first" being the default in text editors, and then you have a "replace all" button if you want to replace everything. That also seems to be Walter's reasoning in std.typetuple.
Jan 22 2011
prev sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
Torarin wrote:
 It sounds like the current replace should be named replaceAll.
That would break lots of existing code, and it doesn't seem to fit. If replacing only the first element means it can take a special range, whereas all other n is is the same, it seems obvious that replaceFirst be the special function (that's what I'd call it too) and normal replace be either: a) Kept exactly as it is b) Add a runtime parameter: string replace(string item, string replaceWhat, string replaceWith, int howMany = int.max); So it stays the same as it is now by default, but can cover any number of items in the same way. If n == infinity was a special case for algorithm or input, it'd make sense to give it a special name (though still big points against it because of inertia). But it isn't. n==1 is special, but n != 1 is always the same. So I'd say go replaceFirst.
Jan 22 2011
prev sibling next sibling parent spir <denis.spir gmail.com> writes:
On 01/22/2011 05:27 PM, Andrei Alexandrescu wrote:
 OK, so we have replace(haystack, needle, nail) which replaces _all_
 occurrences of needle in haystack with nail. How would you call a
 function that replaces only the _first_ occurrence of needle with nail?

 Must be a distinct function, not a runtime parameter to the existing
 function. This is because the function that replaces only one occurrence
 only requires nail to be an input range.
Apart voting for replaceFirst: Python doc: string.replace(str, old, new[, maxreplace])ΒΆ Return a copy of string str with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced. In years and years of string processing, I have used maxreplace once, I guess, and the value was not 1. Does something like replace first belong to stdlib? Even more if it needs be so special. When needed, isnt'it easy to write in two lines using find's result? Also, what is the correct behaviour when not found (what is the one true way)? Denis _________________ vita es estrany spir.wikidot.com
Jan 22 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 1/22/11, spir <denis.spir gmail.com> wrote:
 In years and years of string processing, I have used maxreplace once, I
 guess, and the value was not 1.
Isn't Andrei talking about std.algorithm.replace, which should work with any range and not just strings?
Jan 22 2011
prev sibling parent "Nick Sabalausky" <a a.a> writes:
"Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message 
news:ihf0kc$2fqj$1 digitalmars.com...
 OK, so we have replace(haystack, needle, nail) which replaces _all_ 
 occurrences of needle in haystack with nail. How would you call a function 
 that replaces only the _first_ occurrence of needle with nail?

 Must be a distinct function, not a runtime parameter to the existing 
 function. This is because the function that replaces only one occurrence 
 only requires nail to be an input range.
replaceFirst As for the question of renaming "replace" to "replaceAll", I'd lean more towards "replace", but I honestly don't actually care either way.
Jan 22 2011