Its always nice to complete a challenge in 4clojure.com, especially when you feel you’ve articulated the solution in the most readable form. I’ve just completed the pairwise-disjoint problem, which is pure set algebra: Given a set of sets, find out if these sets have any elements in common. Enough with talk, here’s the code in all its recursive beauty:
(fn pairwise-disjoint [set-of-sets] (or (empty? set-of-sets) (let [examined-set (first set-of-sets) remaining-sets (rest set-of-sets) disjoint-map (map #(clojure.set/intersection examined-set %) remaining-sets) disjoint (every? empty? disjoint-map)] (and disjoint (pairwise-disjoint remaining-sets)) )))
The nice thing about 4Clojure is that it reveals other users’ answers so I just found out you can do the same in one single line, so the solution can be as terse as:
Woa there! I understand that the «imperative» way of thinking prevails throughout my solution, but is this one single line readable? Yes it achieves the same goal, but does the poor soul that’ll read this single line in 20 years from now understand its purpose?