digitalmars.D - more naming
- Andrei Alexandrescu (7/7) Jan 22 2011 OK, so we have replace(haystack, needle, nail) which replaces _all_
- Andrej Mitrovic (3/3) Jan 22 2011 What if you want to replace a _count_ number of occurrences of needle
- Andrei Alexandrescu (5/8) Jan 22 2011 A specific count is rare but can be added as a defaulted parameter. The
- Justin Johansson (10/17) Jan 22 2011 If you articulate this question to yourself over and over until you
- Andrei Alexandrescu (3/23) Jan 22 2011 Now I am feeling lost indeed :o).
- Torarin (3/10) Jan 22 2011 It sounds like the current replace should be named replaceAll.
- so (4/6) Jan 22 2011 replaceAll
- Andrei Alexandrescu (5/19) Jan 22 2011 That's what I'm fearing - changing current replace to replaceAll and
- Daniel Gibson (6/28) Jan 22 2011 IMHO replace (without eny suffix) sounds like it replaces every occurenc...
- bearophile (4/7) Jan 22 2011 OK.
- Paul D. Anderson (3/35) Jan 22 2011 This was my first thought, too.
- Torarin (5/9) Jan 22 2011 Yeah, I see that problem. I'm just so used to "replace first" being
- Adam D. Ruppe (18/19) Jan 22 2011 That would break lots of existing code, and it doesn't seem to
- spir (16/22) Jan 22 2011 Apart voting for replaceFirst:
- Andrej Mitrovic (3/5) Jan 22 2011 Isn't Andrei talking about std.algorithm.replace, which should work
- Nick Sabalausky (5/11) Jan 22 2011 replaceFirst
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
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
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
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. AndreiIf 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
On 1/22/11 11:03 AM, Justin Johansson wrote:On 23/01/11 03:27, Andrei Alexandrescu wrote:Now I am feeling lost indeed :o). AndreiOK, 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. AndreiIf 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
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. AndreiIt sounds like the current replace should be named replaceAll. Torarin
Jan 22 2011
It sounds like the current replace should be named replaceAll. TorarinreplaceAll replaceN replaceFirst replaceLast
Jan 22 2011
On 1/22/11 11:38 AM, Torarin wrote: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. AndreiOK, 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. AndreiIt sounds like the current replace should be named replaceAll. Torarin
Jan 22 2011
Am 22.01.2011 18:46, schrieb Andrei Alexandrescu:On 1/22/11 11:38 AM, Torarin wrote: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, - Daniel2011/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. AndreiOK, 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. AndreiIt sounds like the current replace should be named replaceAll. Torarin
Jan 22 2011
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
Daniel Gibson Wrote:Am 22.01.2011 18:46, schrieb Andrei Alexandrescu:This was my first thought, too. PaulOn 1/22/11 11:38 AM, Torarin wrote: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, - Daniel2011/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. AndreiOK, 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. AndreiIt sounds like the current replace should be named replaceAll. Torarin
Jan 22 2011
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. AndreiYeah, 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
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
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
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
"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