yves Baumes
yves Baumes

Reputation: 9026

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.

Upvotes: 8

Views: 1176

Answers (2)

yves Baumes
yves Baumes

Reputation: 9026

clojure-refactoring could help apparently.

Upvotes: 5

noahlz
noahlz

Reputation: 10311

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

Upvotes: 6

Related Questions