I would appreciate comments on my palindrome predicate function

bool isPalindrome(R)(in R range) @safe pure
    if (isBidirectionalRange!(R))
{
    import std.range: retro, take;
    import std.algorithm: equal;
    static if (hasLength!R)
    {
        const mid = range.length/2; // too long for string
        return equal(range.retro.take(mid),
                     range.take(mid));
    }
    else
    {
        return range.retro.equal(range);
    }
}
unittest
{
    assert("dallassallad".isPalindrome);
    assert(!"ab".isPalindrome);
    assert("a".isPalindrome);
    assert("åäå".isPalindrome);
    assert("".isPalindrome);
    assert([1, 2, 2, 1].isPalindrome);
}
alias isSymmetrical = isPalindrome;

also defined here

https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L773

Specifically I wonder what to do with

    const mid = range.length/2; // TODO too long for string

when R is a string.

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?

Reply via email to