Years ago, Christophe Grand wrote a blog post about how to achieve flatter,
clearer, less-nested code by using a special version of Clojure's cond that
supported :let clauses (just like Clojure's for comprehensions), as well as
:when-let.

I've been using that code on a daily basis ever since, copying it from one
project to another.

For example, here's some code I wrote just today:

(defn- row-type [row]
  (if (set? row) ::row-seq
    (loop [row (seq row), seen (transient #{})]
      (b/cond
        (not row) nil
        :let [item (first row)]
        (not (number? item)) ::row-seq
        (and (not (== item 0)) (not (== item 1))) ::row-seq
        (seen item) ::matrix
        :let [seen (conj! seen item)]
        (recur (next row) seen)))))

There's been a JIRA issue for this for 7 years (
http://dev.clojure.org/jira/browse/CLJ-200), but this feature hasn't yet
made it into Clojure.  That doesn't affect me too much, since I can just
keep using this improved cond in my own source.  But it *does *affect me
when I want to share my code with others, for example, in my open-source
projects.  To avoid confusing other people, I've tried to avoid using this
improved cond in my open-source projects, but once you're used to using
:let inside of cond, it's downright painful to go without, and that pain
was actually stopping me from releasing some of my programs as open-source.

So, in order to begin releasing projects that use this better cond, I've
put better-cond into Clojars.  My convention is that in open-source code,
I'm qualifying the cond with a namespace alias like b/cond as a flag to
readers of the code that this might not be the cond they expect.  In my own
code, I just exclude Clojure's cond (as well as if-let and when-let, for
which improved versions are also offered in this library), and `use` them
directly at the top of every namespace, no qualifying necessary since they
function as drop-in replacements (no real performance hit since the
additional complexity occurs at macro-expansion time).  That way I don't
have to think about it, I just always have the better features at my
disposal.

Now that the library is in Clojars, that means you all can benefit from
this great improvement as well.  Try it, use it, get hooked!  Enjoy.

https://github.com/Engelberg/better-cond

--Mark

P.S. 1.0.1 *removes* a feature from 1.0.0, namely a quirky syntax for
supporting if-let inside of cond, which I've never used, and just felt like
clutter.  But 1.0.0 works perfectly fine, so if that's the version number
that's easier for you to remember, by all means, use it.

-- 
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/d/optout.

Reply via email to