shdqcdsn / counterclockwise

Automatically exported from code.google.com/p/counterclockwise
0 stars 0 forks source link

Detect the indentation periodicity #612

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
When you indent one form out of 2 for example (eg cond or a map).

Sample code:

(defn- unify-indents [a b]
  (reduce-kv 
    (fn [a idx indent]            
      (if-let [a-indent (nth a idx)]
        (if (or (nil? indent) (= a-indent indent))
          a
          (reduced (reduced nil)))
        (assoc a idx indent)))
    a (vec b)))

(defn indent-period [indents]
  (let [indents indents
        n (count indents)]
    (loop [period [] indents indents]
      (when-let [[indent & indents] (and (next indents) (seq indents))]
        (let [period (conj period indent)]
          (or (reduce unify-indents period (partition-all (count period) indents))
            (recur period indents)))))))

Output:
=> (indent-period [0 0])
[0]
=> (indent-period [0 2 0])
[0 2]
=> (indent-period [0 2 0 2])
[0 2]
=> (indent-period [0 nil 0 2 0])
[0 2]
=> (indent-period [1 2 3])
nil

Original issue reported on code.google.com by christophe.grand on 20 Jan 2014 at 10:46

GoogleCodeExporter commented 9 years ago

Original comment by laurent....@gmail.com on 17 Jul 2014 at 1:10