Managing the number of brackets in clojure

I am new to clojure, and the main thing I struggle with is writing readable code. I often get features like the ones below.

(fn rep ([lst n] (rep (rest lst) n (take n (repeat (first lst))))) ([lst n out] (if (empty? lst) out (rep (rest lst) n (concat out (take n (repeat (first lst)))))))) 

with lots of accumulations of trailing brackets. What are the best ways to shorten this or format it in such a way that it is easier to identify missing brackets?

+8
lisp clojure code-readability readability
source share
8 answers

An editor that paints parentheses is extremely useful in this case. For example, here is what your code looks like in my vim editor (using vimclojure ):

Rainbow colors

Since you did not specify which editor you are using, you will need to find the rainbow coloring function for your editor.

+7
source share

Using Emacs paredit (also emulated in several other editors) means that you generally - unless you copy / paste with the mouse / forced-unstructured choices - dealing with matched brackets / curly braces / brackets and corresponding indents without the need for counting .

Emacs with https://github.com/technomancy/emacs-starter-kit (highly recommended!) Is enabled by default for clojure. Otherwise see http://emacswiki.org/emacs/ParEdit

+15
source share

In addition to an editor that supports brace matching, you can also try to make your code less nested. I believe that your function can be rewritten as:

 (defn rep [coll n] (mapcat (partial repeat n) coll)) 

Of course, this is more an art (craft) than a science, but some pointers (in random order):

  • Problems with 4clojure and their solutions to the best users (visible after solving specific problems) - I believe that Chris Houser is under the chouser handle
  • Speaking of CH - “Joy from Clojure” - a very helpful read
  • Viewing documents on clojure.core - there are many useful features.
  • -> and ->> streaming macros are very useful for aligning nested code.
  • stackoverflow - some of the brightest and most helpful people in the world answer questions there; -)
+13
source share
 (fn rep ([lst n] (rep lst n nil)) ([lst n acc] (if-let [s (seq lst)] (recur (rest s) n (concat acc (repeat n (first s)))) acc))) 

it is more readable, I think. note that:

  • you should use recur when tail recursing
  • you should test with seq - see http://clojure.org/lazy
  • repeat can take an amount
  • concat will fall nil, which keeps repeating itself
  • You do not need to run a new line for each open folder

as for parens - your editor / ide should take care of this. I'm typing blind here, so forgive me if this is wrong ...

[Rafał Dowgird code is shorter; I am also learning...]

[updated:] after re-reading the "lazy" link, I think I was processing lazy sequences incorrectly,

+3
source share

I cannot say enough how important it is to use paredit or some similar function in another editor. This frees you from worrying about guys — they always work great, and tedious, error-prone editing tasks such as “change (foo (bar x) y) into (foo (bar xy)) ” become one click away. For a week or so, paredit will disappoint you out of faith, as it prevents you from doing something manually, but once you learn the automatic ways to handle partners, you can never return.

I recently heard someone say, and I think it's pretty accurate that writing lisp without paredit is like writing java without autocomplete (maybe, but not very nice).

+3
source share

I'm not sure if you can escape all the brackets. However, what I saw in Lispers is to use an editor with a match / highlight and possibly even rainbow brackets: http://emacs-fu.blogspot.com/2011/05/toward-balanced-and -colorful-delimiters.html

Honestly, these are functions that would be useful for non-Lisp editors :)

+2
source share

Always use 100% copied sliding parentheses made from at least 75% of the materials after the consumer; then you don’t need to be so bad at using so much.

+1
source share

Format it as you like. The task of the editor is to display the code in any style that the reader prefers. I like the hierarchical tree-like format of C-style with single brackets on their own lines (all LISPers are boiling with rage at the same time :-)))))))))))))

But I sometimes use this style:

  (fn rep ([lst n] (rep (rest lst) n (take n (repeat (first lst)) ) ) ) ) 

which is an update to the traditional style in which the brackets are spaced (log2 level)

The reason I like space is because my eyesight is poor and I just can't read thick text. So, to the angry LISPers who are going to tell me that I am doing what the traditional way, I say, well, everyone has their own way, relaxation, everything is in order.

I can’t wait for someone to write a decent editor in Clojure, although this is not a text editor, but an expression editor **, then the formatting problem disappears. I write myself, but it takes time. The idea is to edit expressions by applying functions to them, and I move the code using zipper, expression by expression, and not by words or characters or lines. The code is represented by any display function you want.

** Yes, I know emacs / paredit, but I tried emacs and I didn’t like regret.

+1
source share

All Articles