Palindrome detection: Difference between revisions

No edit summary
(→‎{{header|jq}}: efficiency)
Line 2,918:
 
=={{header|jq}}==
The definitional implementation would probably be fine except for very long strings:
<syntaxhighlight lang="jq">def palindrome: explode as $in | ($in|reverse) == $in;</syntaxhighlight>
<syntaxhighlight lang="jq">
def palindrome: explode | reverse == .;
</syntaxhighlight>
So here is an implementation with a view to efficiency:
<syntaxhighlight lang="jq">
def isPalindrome:
length as $n
| explode as $in
| first(range(0; $n/2)
| select($in[.] != $in[$n - 1 - .]) )
// false
| not;
</syntaxhighlight>
'''Example''':
"salàlas" | palindrome
2,469

edits