Palindromes

Justin Whear via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Dec 3 14:14:02 PST 2015


On Thu, 03 Dec 2015 21:40:05 +0000, Jim Barnett wrote:

> TL;DR I couldn't figure out how to write `isPalindrome` in terms of
> std.algorithm.mutation.reverse
> 
> I recognize it's more efficient in terms of CPU time and memory than my
> C++ solution, but I suspect there is a shorter expression to detect
> palindromes if efficiency isn't the primary concern. So I am interested
> in seeing implementations of `isPalindrome` that utilize
> `std.algorithm.mutation.reverse`, or anyone who cares to enlighten me
> why that might be a misguided thing to want. Thanks for reading.

I don't think you want reverse because it works in-place; you'd need to 
make a copy to compare against.  std.range.retro is probably what you're 
looking for:

bool isPalindrome(R)(R range) if (isBidirectionalRange!R)
{
	return range.equal(range.retro);
}

Obviously this does more work than strictly necessary, but has the 
brevity you're looking for.


More information about the Digitalmars-d-learn mailing list