How can I refactor clojure source code?

Tag: emacs , clojure , refactoring , elisp Author: gislan Date: 2012-08-30

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? I there refactoring functions in clojure.core which could help? I did not find any...

Other Answer1

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


Yeh thnx for if-let or seq, I know them. Actually the extracted code here is only for illustration purpose. Thank you for the link to Kibit. Actually I am looking into its source code, and discovering new functions I didn't know: walk and prewalk. From the doc it really smell like it's what I was looking for. Thanks :-)

Other Answer2

clojure-refactoring could help apparently.