Similar to this question, here's another implementation. Assume the input is already a set.
(defn powerset [set] (reduce (fn [xs x] (concat xs (map #(cons x %) xs))) [()] set))
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up.Sign up to join this community
There isn't a whole lot here to comment on. I'll just mention a few things:
Technically, from my quick search of what a powerset is, this function should return sets. That seems petty, but unless it's documented to return a lazy list of lazy lists, users may try to treat the "subsets" as sets (like using them as functions). I'd finish this function off by
set over the list.
But to do that, you should rename your parameter, as you're shadowing the build-in
After doing the above two, it developed quite long lines and became nested. I'd add in some use of
->>, and put a few of the lines on the next line.
After that, I ended up with:
(defn powerset [base-set] (->> base-set (reduce (fn [xs x] (concat xs (map #(cons x %) xs))) [()]) (map set)))