## digitalmars.D - findSkip signature

Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
I just added a useful function to Phobos: findSkip. Refer to

http://d-programming-language.org/cutting-edge/phobos/std_algorithm.html#findSkip

and

http://www.dsource.org/projects/phobos/changeset/2339

I'm unsure about the signature. Currently the function returns
Tuple!(R1, "balance", bool, "found"), i.e. the balance of the range
being searched and a Boolean telling whether or not the other range was
found. The Boolean is necessary because returning an empty range is
ambiguous - did you find the needle at the very end of the haystack, or
not at all?

I think a signature that's easier to use is:

bool findSkip(alias pred = "a == b", R1, R2)(ref R1 haystack, R2 needle);

i.e. the haystack is passed by reference and modified if the find was
successful. This is in keep with skipOver's signature, but not in keep
with find's signature.

Opinions?

Andrei
Jan 16 2011
=?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
Andrei Alexandrescu wrote:
I just added a useful function to Phobos: findSkip. Refer to

http://d-programming-language.org/cutting-edge/phobos/std_alg
rithm.html#findSkip

and

http://www.dsource.org/projects/phobos/changeset/2339

I'm unsure about the signature. Currently the function returns
Tuple!(R1, "balance", bool, "found"), i.e. the balance of the range
being searched and a Boolean telling whether or not the other range was
found. The Boolean is necessary because returning an empty range is
ambiguous - did you find the needle at the very end of the haystack, or
not at all?

I think a signature that's easier to use is:

bool findSkip(alias pred = "a == b", R1, R2)(ref R1 haystack, R2 needle);

i.e. the haystack is passed by reference and modified if the find was
successful. This is in keep with skipOver's signature, but not in keep
with find's signature.

Opinions?

Andrei
I am not sure where findSkip is useful but can we assume that the caller is not interested in whether it was found or not? If the callers are interested, they should have the option of calling find and skip separately. If they are always interested, then find and skip should not be merged together. Ali
Jan 17 2011
Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 1/17/11 3:21 PM, Ali Çehreli wrote:
Andrei Alexandrescu wrote:
I just added a useful function to Phobos: findSkip. Refer to

http://d-programming-language.org/cutting-edge/phobos/std_algorithm.html#findSkip

and

http://www.dsource.org/projects/phobos/changeset/2339

I'm unsure about the signature. Currently the function returns
Tuple!(R1, "balance", bool, "found"), i.e. the balance of the range
being searched and a Boolean telling whether or not the other range
was found. The Boolean is necessary because returning an empty range
is ambiguous - did you find the needle at the very end of the
haystack, or not at all?

I think a signature that's easier to use is:

bool findSkip(alias pred = "a == b", R1, R2)(ref R1 haystack, R2 needle);

i.e. the haystack is passed by reference and modified if the find was
successful. This is in keep with skipOver's signature, but not in keep
with find's signature.

Opinions?

Andrei
I am not sure where findSkip is useful but can we assume that the caller is not interested in whether it was found or not? If the callers are interested, they should have the option of calling find and skip separately. If they are always interested, then find and skip should not be merged together.
findSkip is useful in parsing. One simple case in which it was useful was count() that counts how many times a forward range occurs in another. You can't do that with find(). (Try it!) I made the executive decision to pass by reference and return a bool. http://d-programming-language.org/cutting-edge/phobos/std_algorithm.html#findSkip Andrei
Jan 17 2011
Graham St Jack <Graham.StJack internode.on.net> writes:
On 18/01/11 08:04, Andrei Alexandrescu wrote:
On 1/17/11 3:21 PM, Ali Çehreli wrote:
Andrei Alexandrescu wrote:
I just added a useful function to Phobos: findSkip. Refer to

http://d-programming-language.org/cutting-edge/phobos/std_alg
rithm.html#findSkip

and

http://www.dsource.org/projects/phobos/changeset/2339

I'm unsure about the signature. Currently the function returns
Tuple!(R1, "balance", bool, "found"), i.e. the balance of the range
being searched and a Boolean telling whether or not the other range
was found. The Boolean is necessary because returning an empty range
is ambiguous - did you find the needle at the very end of the
haystack, or not at all?

I think a signature that's easier to use is:

bool findSkip(alias pred = "a == b", R1, R2)(ref R1 haystack, R2
needle);

i.e. the haystack is passed by reference and modified if the find was
successful. This is in keep with skipOver's signature, but not in keep
with find's signature.

Opinions?

Andrei
I am not sure where findSkip is useful but can we assume that the caller is not interested in whether it was found or not? If the callers are interested, they should have the option of calling find and skip separately. If they are always interested, then find and skip should not be merged together.
findSkip is useful in parsing. One simple case in which it was useful was count() that counts how many times a forward range occurs in another. You can't do that with find(). (Try it!) I made the executive decision to pass by reference and return a bool.
vote++
http://d-programming-language.org/cutting-edge/phobos/std_alg
rithm.html#findSkip

Andrei
-- Graham St Jack
Jan 17 2011