Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
Created August 28, 2012 15:51
Show Gist options
  • Save pyrtsa/3499353 to your computer and use it in GitHub Desktop.
Save pyrtsa/3499353 to your computer and use it in GitHub Desktop.
Too many parentheses, you say?

Too many parentheses, you say?

FWIW, here are a few common answers to clear your worries about parentheses in Clojure in particular:

  1. It's idiomatic to use indentation to display what's nested within a given set of parentheses. The preferred way to indent code is documented fairly well. [Edit: A few examples are found e.g. here: http://clojure-euler.wikispaces.com/Problem+001]

  2. If you're concerned about matching parens somewhere (i.e. you suspect things aren't correctly indented), use your text editor to blink the matching bracket and fix the indents if needed.

  3. Typical function definitions in Clojure are somewhere between 1 and 20 lines — there's isn't much to match. In case you have more, break things to new functions (or more likely, use the core library functions better to your advantage).

  4. If you end up with code with lots of closing parens in a row:)))))) you have probably done something wrong. For an example, compare the code in this StackOverflow question: http://stackoverflow.com/q/10095264 — and this answer to it: http://stackoverflow.com/a/10095887

  5. Unlike many LISPs, Clojure uses different brackets for different purposes — (function call), [vector of things], '(list of things), {:key "value" :pairs 123}, #(lambda function), #{:set :of :things}, "a string, of course"… — so it's easier to match what things pair up with.

  6. The clojure.core library provides far more basic utilities for general-purpose data processing than you've probably used to in your Java/JavaScript/C/C++/Python/Ruby language of choice. And you get to use those same functions over and over again, since most functions can be used over a large set of different types input. What does this have to do with parentheses? You get to write less of them, since you get to avoid those loops and conditionals you were prepared to write when you started.

But it's obvious that getting to that point demands a little bit of practice and willingness to seeking simpler solutions to the problem at hand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment