The code design in Clojure

Every time he searched for learning Clojure has noticed that to read well formatted code after some training it becomes easy. But solving problems from project Euler, formatting such a “right” way to code, for me was a big problem.
No explanation in Russian, I did not find, therefore decided to fill the gap by translating a short manual from English.

In essence, the Clojure code is just a set parenthesesliterals, because without competent the indentation difficult to read. Clojure with the indentation looks like this:
the
(defn my-zipmap [keys vals]
(loop [map {}
ks (seq keys)
vs (seq vals)]
(if (and ks vs)
(recur (assoc map (first ks) (first vs))
(rest ks)
(rest vs))
map)))
(my-zipmap [:a :b :c] [1 2 3])


The basic rule is: the continuation of the list on a new line indented writing. So, the line starting with (loop is indented 2 spaces relative to the row (defn because the list starts with (loop is a direct element of the list starting with (defn.
However, below the author of the code derogates from the basic rule, aligning (sets and (and, rather than retreating from space 2 (if.
Also a vector starting with [map occurs inside a string (not a new line) because the 2 following lines aligned at the position following [

In the result the rules are as follows:
1. Detachable continuation lines by two spaces if not applicable, the following rules
2. If possible, align indentation instead of the parentheses (, [ or {
3. With continued internal list (list nocatalog from the beginning of the line) align the rows by the first characters of the items in this list

In fact, the alignment always takes place relative to the brackets (, [ or { which belongs to align the element.
Of course without some of the habit of reading and writing in such a scheme difficult.

For Lisp s typically an abundance of closing parentheses:

the
(defn supply-arg [arg flist]
(loop [flist flist cnter 0]
(if (first flist) (do ((first flist) arg) 
(recur (rest flist) (+ 1 cnter)))))) 

To cope with them and dotting otstupny help text editors imprisoned under the edit Clojure.

To start everything. It makes sense to continue to upload articles on Clojure?
Here: en.wikibooks.org/wiki/Learning_Clojure/Coding_Conventions
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Integration of PostgreSQL with MS SQL Server for those who want faster and deeper

Custom database queries in MODx Revolution

Parse URL in Zend Framework 2