Permalink
Browse files

Unchunked range in subsets

  • Loading branch information...
1 parent a99dc97 commit 4b5312218344264c3227f7f814db6c688d0ab2fc @Engelberg Engelberg committed Nov 13, 2013
Showing with 19 additions and 5 deletions.
  1. +5 −3 README.md
  2. +14 −2 src/main/clojure/clojure/math/combinatorics.clj
View
@@ -9,7 +9,7 @@ sequences for common combinatorial functions.
Releases and Dependency Information
========================================
-Latest stable release: 0.0.6
+Latest stable release: 0.0.7
* [All Released Versions](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.clojure%22%20AND%20a%3A%22math.combinatorics%22)
@@ -18,7 +18,7 @@ Latest stable release: 0.0.6
[Leiningen](https://github.com/technomancy/leiningen) dependency information:
```clojure
-[org.clojure/math.combinatorics "0.0.6"]
+[org.clojure/math.combinatorics "0.0.7"]
```
[Maven](http://maven.apache.org/) dependency information:
@@ -27,7 +27,7 @@ Latest stable release: 0.0.6
<dependency>
<groupId>org.clojure</groupId>
<artifactId>math.combinatorics</artifactId>
- <version>0.0.6</version>
+ <version>0.0.7</version>
</dependency>
```
@@ -102,6 +102,8 @@ Developer Information
Changelog
========================================
+* Release 0.0.7 on 2013-11-13
+ * Unchunk range in `subsets` to minimize memory usage.
* Release 0.0.6 on 2013-10-31
* Removed use of mapv for backwards compat with older versions of Clojure.
@@ -2,7 +2,7 @@
;;; sequences for common combinatorial functions.
;; by Mark Engelberg (mark.engelberg@gmail.com)
-;; Last updated - October 31, 2013
+;; Last updated - November 13, 2013
(ns
#^{:author "Mark Engelberg",
@@ -95,12 +95,24 @@ Most of these algorithms are derived from algorithms found in Knuth's wonderful
(= n cnt) (list (seq items))
: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"

0 comments on commit 4b53122

Please sign in to comment.