January 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.
>
> 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.
January 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
January 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

January 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?
January 22, 2011
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
January 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.


1 2
Next ›   Last »