Skip to content

Commit

Permalink
Unchunked range in subsets
Browse files Browse the repository at this point in the history
  • Loading branch information
Engelberg committed Nov 13, 2013
1 parent a99dc97 commit 4b53122
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
8 changes: 5 additions & 3 deletions README.md
Expand Up @@ -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)

Expand All @@ -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:
Expand All @@ -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>
```

Expand Down Expand Up @@ -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.

Expand Down
16 changes: 14 additions & 2 deletions src/main/clojure/clojure/math/combinatorics.clj
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 4b53122

Please sign in to comment.