Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Palindrome Goodie #83

Merged
merged 4 commits into from May 3, 2012
Merged

Palindrome Goodie #83

merged 4 commits into from May 3, 2012

Conversation

ghost
Copy link

@ghost ghost commented May 1, 2012

Very simple, displays whether or not a string is a palindrome. Queried like so:

'is (query) a[n] a palindrome[?]' and 'isPalindrome (query)'

Results look like: '(query) is a palindrome!'

I'm new to Perl so please let me know if I did something stupid.

Thanks!

@crazedpsyc
Copy link
Contributor

There are a few problems, so I'll show you what I would do and then walk through it.

triggers any => "palindrome";

handle query_clean => sub {

    #Remove the trigger text from the query.
    return unless /^is \s+ (\S+) \s+ an? \s* palindrome\??$/ix;

    #Check to see if it is a palindrome.
    return ($1 eq scalar reverse $1) ? "$1 is a palindrome." : "$1 is not a palindrome";
};

First thing I am doing differently is with that trigger. regexp triggers are actually a lot slower than words, so we try to use words wherever possible.
Then in the handler I use a regexp to make sure I have proper input. In regular expressions, () are capturing groups, so the (\S+) captures the word we plan to use (\S is anything but space, so a single word.)
At the end, I use a ternary operation to give the result.

@ghost
Copy link
Author

ghost commented May 2, 2012

Awesome, thanks! Looks good, I just have a few clarifications:

From what I understand, $_ is a local variable scoped within closures; evaluating a regex will use $_ as a parameter implicitly, correct? And $1 is representing the 1st capturing group? ( whatever matches \S+)

My code was a bit verbose due to my first time using Perl, your code is much better!

@crazedpsyc
Copy link
Contributor

As I understand it, $_ is set automatically by for loops, but DDG sets it explicitly for the handler subroutine. /foo/; and s/foo/bar/; are the same as $_ =~ ..., and you are also correct on $1.
I could be wrong about $_ though, I am also a perl noob :)

@ghost
Copy link
Author

ghost commented May 2, 2012

I looked it up last night, $_ is the default variable for functions in Perl.

In the sub, $_ contains the value of the arguments passed in. It is also passed to functions with arguments during a call with no arguments. So, invoking regex without explicitly passing a source uses $_.

Easy enough but not very intuitive IMO. Perl kinda feels like assembly, the $1-$9 variables etc. I'm headed into to work in a bit, I'll implement the improvements. Thanks again!

@crazedpsyc crazedpsyc merged commit 470ce3c into duckduckgo:master May 3, 2012
@moollaza
Copy link
Member

@bm1362 Hey there, when you get a chance, would you mind sending an email to moollaza@duckduckgo.com please? Or if you don't mind, can I have your email address? (We have some goodies for our contributors!) Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants