I am very new to clojure and I haven't done a ton of lisp yet. 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 bad code, because the two are almost the same function call is suboptimal, where the only difference - (.indexOf scale degree)vs degree.
What is a Clojure / Lisp Method to remove this duplication of code? I feel this should include a trifle, but I'm not sure. Any other common pointers associated with this block of code are also appreciated.
Edit: I reanalyzed the code as suggested by andrew cooke, now the function 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.
source
share