Clojure and me has moved.

Wednesday, October 29, 2008

Clojure Golf: fib-seq

This post has moved, go to its new location
Revisiting a classic:
(def fib-seq
(lazy-cat [0 1] (map + fib-seq (rest fib-seq))))

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))

Monday, October 20, 2008

Clojure Golf: subsets-by-card

This post has moved, go to its new location
(defn subsets-by-card [s]
(reduce (fn [ssbc x]
(concat
(map (fn [a b] (concat a (map #(conj % x) b)))
(cons nil ssbc) ssbc)
[[#{}]]))
[[#{}]] s))

(a tough one)

Clojure Golf: subsets

This post has moved, go to its new location
(defn subsets [s]
(reduce (fn [ss x] (concat ss (map #(conj % x) ss))) [#{}] s))

Sunday, October 19, 2008

Clojure Golf: combinations (2)

This post has moved, go to its new location
(defn combinations [& cs]
(reduce #(for [v %1 i %2] (conj v i)) [[]] cs))

Saturday, October 18, 2008

Clojure Golf: combinations

This post has moved, go to its new location
(defn combinations [& cs]
(reduce (fn [vs c]
(mapcat #(map conj vs (repeat %)) c))
[[]] cs))