Clojure and me has moved.

Wednesday, October 22, 2008

Clojure Golf: subsets-by-card (2)

This post has moved, go to its new location
I wasn't happy with the last one. At least this one is lazier and in increasing cardinality order.
(defn subsets-by-card [s]
(reduce (fn [ssbc x]
(map (fn [a b] (concat a (map #(conj % x) b)))
(concat ssbc [nil]) (concat [nil] ssbc)))
[[#{}]] s))

Decreasing order:
(defn subsets-by-card-reverse [s]
(reduce (fn [ssbc x]
(map (fn [a b] (concat a (map #(disj % x) b)))
(concat ssbc [nil]) (concat [nil] ssbc)))
[[(set s)]] s))

No comments: