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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With