Skip to content

Commit 4b53122

Browse files
committed
Unchunked range in subsets
1 parent a99dc97 commit 4b53122

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ sequences for common combinatorial functions.
99
Releases and Dependency Information
1010
========================================
1111

12-
Latest stable release: 0.0.6
12+
Latest stable release: 0.0.7
1313

1414
* [All Released Versions](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.clojure%22%20AND%20a%3A%22math.combinatorics%22)
1515

@@ -18,7 +18,7 @@ Latest stable release: 0.0.6
1818
[Leiningen](https://github.com/technomancy/leiningen) dependency information:
1919

2020
```clojure
21-
[org.clojure/math.combinatorics "0.0.6"]
21+
[org.clojure/math.combinatorics "0.0.7"]
2222
```
2323

2424
[Maven](http://maven.apache.org/) dependency information:
@@ -27,7 +27,7 @@ Latest stable release: 0.0.6
2727
<dependency>
2828
<groupId>org.clojure</groupId>
2929
<artifactId>math.combinatorics</artifactId>
30-
<version>0.0.6</version>
30+
<version>0.0.7</version>
3131
</dependency>
3232
```
3333

@@ -102,6 +102,8 @@ Developer Information
102102

103103
Changelog
104104
========================================
105+
* Release 0.0.7 on 2013-11-13
106+
* Unchunk range in `subsets` to minimize memory usage.
105107
* Release 0.0.6 on 2013-10-31
106108
* Removed use of mapv for backwards compat with older versions of Clojure.
107109

src/main/clojure/clojure/math/combinatorics.clj

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
;;; sequences for common combinatorial functions.
33

44
;; by Mark Engelberg (mark.engelberg@gmail.com)
5-
;; Last updated - October 31, 2013
5+
;; Last updated - November 13, 2013
66

77
(ns
88
#^{:author "Mark Engelberg",
@@ -95,12 +95,24 @@ Most of these algorithms are derived from algorithms found in Knuth's wonderful
9595
(= n cnt) (list (seq items))
9696
:else
9797
(map #(map v-items %) (index-combinations n cnt)))))))
98+
99+
(defn- unchunk
100+
"Given a sequence that may have chunks, return a sequence that is 1-at-a-time
101+
lazy with no chunks. Chunks are good for efficiency when the data items are
102+
small, but when being processed via map, for example, a reference is kept to
103+
every function result in the chunk until the entire chunk has been processed,
104+
which increases the amount of memory in use that cannot be garbage
105+
collected."
106+
[s]
107+
(lazy-seq
108+
(when (seq s)
109+
(cons (first s) (unchunk (rest s))))))
98110

99111
(defn subsets
100112
"All the subsets of items"
101113
[items]
102114
(mapcat (fn [n] (combinations items n))
103-
(range (inc (count items)))))
115+
(unchunk (range (inc (count items))))))
104116

105117
(defn cartesian-product
106118
"All the ways to take one item from each sequence"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy