I wrote a function to compute the symmetric difference of two sets (one of the problems on the 4clojure site). The function passed the unit tests, but it’s not as clean as I would like, given that I have duplicated code.
(fn [x y] (set (concat
(keep-indexed #(if (nil? (get y %2)) %2) x)
(keep-indexed #(if (nil? (get x %2)) %2) y))))
Obviously I would prefer something like:
(fn [x y] (set (concat (diff x y) (diff y x))))
Where the diff function is defined and referenced “inline”, but I don’t know how to do that in one fn block.
Use a
letorletfn: