findSkip signature

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Jan 17 13:34:38 PST 2011


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



More information about the Digitalmars-d mailing list