Clojure Golf: subsets-by-card (2)

unsorted — cgrand, 22 October 2008 @ 10 h 14 min

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 #(<ins>disj</ins> % x) b)))
              (concat ssbc [nil]) (concat [nil] ssbc)))
    [[<ins>(set s)</ins>]] s))


No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

(c) 2024 Clojure and me | powered by WordPress with Barecity