Skip to content
Browse files

Fix subsets OutOfMemoryError

  • Loading branch information...
1 parent be1b13b commit 5adb2fdfd6e1661eb4056cbf06e8d4a633dd0088 @xpe committed Jun 23, 2013
Showing with 15 additions and 2 deletions.
  1. +13 −1 src/clojure/math/combinatorics.clj
  2. +2 −1 test/clojure/math/test_combinatorics.clj
View
14 src/clojure/math/combinatorics.clj
@@ -93,11 +93,23 @@
:else
(map #(map v-items %) (index-combinations n cnt)))))))
+(defn- unchunk
+ "Given a sequence that may have chunks, return a sequence that is 1-at-a-time
+ lazy with no chunks. Chunks are good for efficiency when the data items are
+ small, but when being processed via map, for example, a reference is kept to
+ every function result in the chunk until the entire chunk has been processed,
+ which increases the amount of memory in use that cannot be garbage
+ collected."
+ [s]
+ (lazy-seq
+ (when (seq s)
+ (cons (first s) (unchunk (rest s))))))
+
(defn subsets
"All the subsets of items"
[items]
(mapcat (fn [n] (combinations items n))
- (range (inc (count items)))))
+ (unchunk (range (inc (count items))))))
(defn cartesian-product
"All the ways to take one item from each sequence"
View
3 test/clojure/math/test_combinatorics.clj
@@ -8,7 +8,8 @@
(deftest test-subsets
(are [x y] (= x y)
- (subsets [1 2 3]) '(() (1) (2) (3) (1 2) (1 3) (2 3) (1 2 3))))
+ (subsets [1 2 3]) '(() (1) (2) (3) (1 2) (1 3) (2 3) (1 2 3)))
+ (last (subsets (range 0 21))) (range 0 21))
(deftest test-cartesian-product
(are [x y] (= x y)

0 comments on commit 5adb2fd

Please sign in to comment.
Something went wrong with that request. Please try again.