On Friday, 24 October 2014 at 22:29:12 UTC, Peter Alexander wrote:
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.

Clever. Thx!

Reply via email to