The condp form is very nice and concise if you have multiple match clauses. 
If you are more generally just looking to perform a single 
match/assign/branch task, I'd recommend this little nugget of clojure 
wisdom: Forget ye not the hidden might of if-let.

(if-let [[_ from to message] (re-find #"^:(.*?)!.*PRIVMSG (.*) :(.*)" msg)]
  (true-branch)
  (false-branch))

If you use an expression that returns a sequence with if-let, just wrap it 
in a call to seq, like so:

(if-let [[a b c d] (seq (filter anything-good? some-coll))]
  (true-branch)
  (false-branch))

Happy hacking,
  ~Gary

On Friday, June 21, 2013 11:43:50 AM UTC-4, Steven Arnold wrote:
>
> Hi, I am writing a simple IRC bot, pretty much just for fun, starting with 
> a simple implementation originally posted by Nurullah Akkaya on his blog. 
>  It already does what it's supposed to, which is message a fortune from 
> mod-fortune (shelling out) when someone asks it to. 
>
> However, there's a bit of code I don't like.  It looks like this: 
>
>      (cond 
>        (re-find #"^:(.*?)!.*PRIVMSG (.*) :(.*)" msg) 
>          (let [[_ from to message] (re-find #"^:(.*?)!.*PRIVMSG (.*) 
> :(.*)" msg)] 
>            [...logic omitted...] 
>
> What's happening is we're looking for a PRIVMSG, and if we find one, we 
> scan the message again and pull out the chunks we're interested in, and use 
> them in the "logic" part below. 
>
> The problem is I don't like maintaining two regular expressions.  I wish I 
> could use it only once, and therefore change it only once if it needs to be 
> modified.  I'd prefer to see an expression that looks like this (for 
> example): 
>
> (cond 
>  (if-matched #"^:(.*?)!.*PRIVMSG (.*) :(.*)" msg 
>    [from to message] 
>    (true form) 
>    (optional false form))) 
>
> The if-matched acts as a let block while pulling out groups and assigning 
> to local variables at the same time.  Also, it ignores the first "match" of 
> re-find, which is the entire expression -- not generally useful to me. 
>  Maybe if-matched-groups would ignore the overall expression, and 
> if-matched would include it. 
>
> I suppose a macro might be the way to go to accomplish this, but I wonder 
> if there are any Clojure tricks that could accomplish this short of a 
> macro.  Also, do any fellow Clojurians think a macro like this is a bad 
> idea in general?  Would you suggest taking a different tack to solve this 
> problem? 
>
> Thanks in advance! 
> steven

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to