Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I refactor Clojure source code?

Doing 4clojure exercices, one goal is to provide a correct answer and an optional goal is to provide the shortest solution as possible (spaces are not counted in), a.k.a code golf. You can then compare to the others solutions.

When doing an exercice I first find a correct answer. Then I apply a manual refactoring to my source code, which consists in renaming variables names into a single character. For instance, after applying this basic step, my code may look like this.

(fn f[s]
   (if (empty? s)
     {}
     (let [[k & r] s
        [v n] (split-with number? r)]
        (assoc (f n) k v))))

Quite unreadable for a human, but score better in 4Clojure code golf contest since it saves a lot of characters.

For the sake of curiosity how would you do in emacs to automate that? Is there refactoring functions in clojure.core which could help? I did not find any.

like image 706
yves Baumes Avatar asked Sep 15 '12 17:09

yves Baumes


1 Answers

Learn the libraries. For example, it looks like you could use if-let or seq. If you don't consider using a static analysis tool "cheating", check out Kibit

like image 131
noahlz Avatar answered Oct 17 '22 07:10

noahlz