Jump to page: 1 2
Thread overview
more naming
Jan 22, 2011
Andrej Mitrovic
Jan 22, 2011
Justin Johansson
Jan 22, 2011
Torarin
Jan 22, 2011
so
Jan 22, 2011
Daniel Gibson
Jan 22, 2011
bearophile
Jan 22, 2011
Paul D. Anderson
Jan 22, 2011
Torarin
Jan 22, 2011
Adam D. Ruppe
Jan 22, 2011
spir
Jan 22, 2011
Andrej Mitrovic
Jan 22, 2011
Nick Sabalausky
January 22, 2011
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
January 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.
January 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.
>
>
> 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
January 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
January 22, 2011
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
January 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.
>
>
> Andrei
>

It sounds like the current replace should be named replaceAll.

Torarin
January 22, 2011
> It sounds like the current replace should be named replaceAll.
>
> Torarin

replaceAll
replaceN
replaceFirst
replaceLast
January 22, 2011
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
January 22, 2011
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
January 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.
« First   ‹ Prev
1 2