I'm very new to clojure, and I haven't done a ton of lisp previously. I have a function that contains the following:
(defn chord
([scale degree num_voices]
(if
(keyword? degree)
(take num_voices (take-nth 2 (cycle (invert scale (.indexOf scale degree)))))
(take num_voices (take-nth 2 (cycle (invert scale degree))))))
Obviously, this code is poor because having two almost-identical function calls here is suboptimal, where the only difference is (.indexOf scale degree)
vs degree
.
What's the Clojure/Lisp Way of removing this code duplication? I feel like it should involve a let, but I'm not positive. Any other general pointers related to this code block are also appreciated.
Edit: I have re-factored the code according to andrew cooke's suggestion, the function now reads:
(defn chord
([scale degree num_voices]
(let [degree (if (keyword? degree) (.indexOf scale degree) degree)]
(take num_voices (take-nth 2 (cycle (invert scale degree))))
)
)
Thanks to everyone who answered so quickly.