Single pull request can make a difference in your learning of Clojure

It did for me a few times already.

Learning a new programming language involves many techniques to become proficient in its use – reading books, katas, reading others’ source code and finally using the language on a daily basis, hopefully in a commercial project with other geeks who’re already familiar with the concepts and can ensure the success of the project.

I got used to sip Clojure’s knowledge from the Notifications tab in GitHub for a few Clojure projects – just mark a project Watched (right-hand side at the top of the project) and go to the Notifications tab. So easy that I’m pretty sure you’ve already been quite accustomed to it. If you haven’t, you should!

With the recent pull request for leiningen, I again have been exposed to something I think emerges as a new programming style (shall I say an idiom) in Clojure – using and or or (or doubled on purpose) instead of if or when. It makes the code so much concise, although it takes time to get used to it. I’m the one who haven’t yet, but at least can spot its use.

So, instead of using if for a single conditional branch:

(if main
  (some #(.exists (io/file % (b/path-for main))) source-paths)))

you can do this with and:

(and main (some #(.exists (io/file % (b/path-for main))) source-paths))

It’s semantically the same, but the and construct looks more geeky or idiomatic, doesn’t it?

You could also use when for a single conditional branch with the added benefit of not having to use the do macro to call more functions (when side effects do matter).

(when main
  (some #(.exists (io/file % (b/path-for main))) source-paths)))

The single line of code has also reminded me about the two other functions in Clojure I haven’t got used to use that often yet – some and every?

([pred coll])
  Returns the first logical true value of (pred x) for any x in coll,
  else nil. One common idiom is to use a set as pred, for example
  this will return :fred if :fred is in the sequence, otherwise nil:
  (some #{:fred} coll)

([pred coll])
  Returns true if (pred x) is logical true for every x in coll, else

They’re so simple yet I can’t remember their use and used to forget them so much often. Perhaps time for a resolution for 2013 to make them remembered?

Done. Thanks hyPiRion! Reading your pull requests makes my days.

Be Sociable, Share!
This entry was posted in Languages.

6 Responses to Single pull request can make a difference in your learning of Clojure

  1. Honestly NO, the “and” does NOT look more idiomatic, it might be more geeky not idiomatic.
    If I find an “and” I am expecting to check at least two things to be true, not just one.
    However “and” does something different, it evaluate every expression you pass and return the last expres that doesn’t return true, or the very last expression.
    Use and in that way I think is a bad hack around, what if tomorrow we decide to change and in such way that does return only true or false ? That code will be broken…

    However “when” in this case is way better, it is just the perfect fit for the “when” statement, you don’t have a false branch and if main in true you evaluate the body, just what when is suppose to do, with the benefit, how you pointed out, that when does it in an implicit “do” so if you want to change your code or you need some side effects for any reason (debug maybe ?) you won’t find any problem.

    • Your response is definitely something that made me think about ‘and’ again. For the moment what I must admit is that you’re indeed right that it might be misleading.

      Thanks for sharing your take on it. It helps developing my self-confidence in FP much better!

  2. wagjo says:

    I think you are wrong about the usage of and. Using and instead of if is not idiomatic nor geeky.

    There are situations where you can use any of “and”, “cond”, “condp”, “if”, “if-not”, “when” or “when-not”. What is important is not the geekiness but the meaning of what you are trying to articulate in the code. Its the semantics which matters.

    For example take this pull request you are refering to. “and” is used in the predicate function, which returns a logical value. “and” is used because both of the expressions must be true in order for predicate function to return true.

  3. Pingback: About Functional Web Architecture (with Ring and Compojure) by James Reeves | Japila :: verba docent, exempla trahunt

Leave a Reply

%d bloggers like this: