Reflections on isPalindrome
    Peter Alexander via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Fri Oct 24 15:29:11 PDT 2014
    
    
  
On Friday, 24 October 2014 at 21:56:20 UTC, Nordlöw wrote:
> bool isPalindrome(R)(in R range) @safe pure
Aside: for templates, just let the compiler infer @safe and pure. 
You don't know whether the range operations on R are pure or not.
As for the actual algorithm, there's no need for the random 
access version, and you bidirectional version does twice as much 
as necessary:
Just do:
while (!range.empty)
{
   if (range.front != range.back) return false;
   range.popFront();
   if (range.empty) break;
   range.popBack();
}
return true;
This automatically handles narrow strings.
> Further, I would like to extend isPalindrome() with a minimum 
> length argument minLength that for string and wstring does
>
> import std.uni: byDchar;
> range.byDchar.array.length >= minLength.
>
> AFAIK this will however prevent my algorithm from being 
> single-pass right?
I'm not sure what you are saying here, but hopefully the above 
code obviates this anyway.
    
    
More information about the Digitalmars-d-learn
mailing list