Isn't using find with retro awkward?

spir denis.spir at gmail.com
Wed Feb 16 01:31:39 PST 2011


On 02/16/2011 07:22 AM, Denis Koroskin wrote:
> On Tue, 15 Feb 2011 06:35:21 +0300, Andrej Mitrovic <none at none.none> wrote:
>
>> import std.stdio, std.algorithm, std.range;
>>
>> void main()
>> {
>> writeln( find([5, 1, 2, 3, 4, 5, 1], 5) );
>> writeln( find(retro([5, 1, 2, 3, 4, 5, 1]), 5) );
>> }
>>
>> Output:
>> [5, 1, 2, 3, 4, 5, 1]
>> [5, 4, 3, 2, 1, 5]
>>
>> The docs for find are:
>> "returns : haystack advanced such that binaryFun!pred(haystack.front, needle)
>> is true "
>> "To find the last occurence of needle in haystack, call find(retro(haystack),
>> needle). "
>>
>> To me, if I was looking for the last element in a range I would expect to get
>> a range with the found element followed by an elements after it. Obviously
>> retro reverses the range (it just hard-wires front=back, back=front for those
>> not in the know), so this code is correct.
>>
>> Still, I would expect that finding a last element in this range:
>> [5, 1, 2, 3, 4, 5, 1]
>>
>> would return the range:
>> [5, 1]
>>
>> and not:
>> [5, 4, 3, 2, 1, 5]
>>
>> Isn't that what most people would want when they're looking for the last
>> matching element?
>
> Try retro(find(retro(haystack, needle)));

for any reason, I would prefere
     findBack(haystack, needle);
:-)
Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list