File: README.md

package info (click to toggle)
math-combinatorics-clojure 0.1.4-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 164 kB
  • ctags: 3
  • sloc: makefile: 37; xml: 25
file content (216 lines) | stat: -rw-r--r-- 6,103 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
clojure.math.combinatorics
========================================

Formerly clojure.contrib.combinatorics.

Efficient, functional algorithms for generating lazy
sequences for common combinatorial functions.

Releases and Dependency Information
========================================

Latest stable release: 0.1.4

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

* [Development Snapshot Versions](https://oss.sonatype.org/index.html#nexus-search;gav~org.clojure~math.combinatorics~~~)

[Leiningen](https://github.com/technomancy/leiningen) dependency information:

```clojure
[org.clojure/math.combinatorics "0.1.4"]
```

[Maven](http://maven.apache.org/) dependency information:

```xml
<dependency>
  <groupId>org.clojure</groupId>
  <artifactId>math.combinatorics</artifactId>
  <version>0.1.4</version>
</dependency>
```

*Note: If you are using Clojure 1.2 - 1.6, you will need math.combinatorics version 0.1.3.*

Example Usage
========================================

All functions return lazy sequences.

```clojure
(ns example.core
  (:require [clojure.math.combinatorics :as combo]))

; PERMUTATIONS
; all the unique arrangements of items
=> (combo/permutations [1 2 3])
([1 2 3] [1 3 2] [2 1 3] [2 3 1] [3 1 2] [3 2 1])

; Note that permutations intelligently handles duplicate items
=> (combo/permutations [1 1 2])
([1 1 2] [1 2 1] [2 1 1])

; These functions are more efficient than calling count, nth, drop
; on the underlying sequence
=> (combo/count-permutations [1 2 3])
6
=> (combo/count-permutations [1 1 2])
3
=> (combo/nth-permutation [1 2 3] 3)
[2 3 1]
=> (combo/nth-permutation [1 1 2 2] 5)
[2 2 1 1]
=> (combo/drop-permutations [1 2 3] 3)
([2 3 1] [3 1 2] [3 2 1])

; For a sortable collection of items, you can find out where it is
; in the lexicographic sequence of permutations
=> (combo/permutation-index [\a \b \a \c \a \b])
16
=> (combo/nth-permutation [\a \a \a \b \b \c] 16)
[\a \b \a \c \a \b]


; COMBINATIONS
; all the unique ways of taking t different elements from items
(combo/combinations [1 2 3] 2)
;;=> ((1 2) (1 3) (2 3))

; Note that combinations intelligently handles duplicate items
; treating the input list as a representation of a 'multiset'
 => (combo/combinations [1 1 1 2 2] 3)
((1 1 1) (1 1 2) (1 2 2))

; These functions are more efficient than calling count and nth
; on the underlying sequence
=> (combo/count-combinations [1 1 1 2 2] 3)
3
=> (combo/nth-combination [1 2 3 4 5] 2 5)
[2 4]

; SUBSETS
; all the subsets of items
=> (combo/subsets [1 2 3])
(() (1) (2) (3) (1 2) (1 3) (2 3) (1 2 3))

; Note that subsets intelligently handles duplicate items
; treating the input list as a representation of a 'multiset'
=> (combo/subsets [1 1 2])
(() (1) (2) (1 1) (1 2) (1 1 2))

; These functions are more efficient than calling count and nth
; on the underlying sequence
=> (combo/count-subsets [1 1 2])
6
=> (combo/nth-subset [1 1 2] 3)
[1 1]

; CARTESIAN PRODUCT
; all the ways to take one item from each passed-in sequence
=> (combo/cartesian-product [1 2] [3 4])
((1 3) (1 4) (2 3) (2 4))

; SELECTIONS
; all the ways to take n (possibly the same) items from the sequence of items
=> (combo/selections [1 2] 3)
((1 1 1) (1 1 2) (1 2 1) (1 2 2) (2 1 1) (2 1 2) (2 2 1) (2 2 2))

; PARTITIONS
; all the partitions of items.
=> (combo/partitions [1 2 3])
(([1 2 3])
 ([1 2] [3])
 ([1 3] [2])
 ([1] [2 3])
 ([1] [2] [3]))

 ; Note that partitions intelligently handles duplicate items
=> (combo/partitions [1 1 2])
(([1 1 2])
 ([1 1] [2])
 ([1 2] [1])
 ([1] [1] [2]))

 ; You can also specify a min and max number of partitions
(combo/partitions [1 1 2 2] :min 2 :max 3)
(([1 1 2] [2])
 ([1 1] [2 2])
 ([1 1] [2] [2])
 ([1 2 2] [1])
 ([1 2] [1 2])
 ([1 2] [1] [2])
 ([1] [1] [2 2]))
```

Refer to docstrings in the `clojure.math.combinatorics` namespace for
additional documentation.

[API Documentation](http://clojure.github.com/math.combinatorics/)

Developer Information
========================================

* [GitHub project](https://github.com/clojure/math.combinatorics)

* [Bug Tracker](http://dev.clojure.org/jira/browse/MCOMB)

* [Continuous Integration](http://build.clojure.org/job/math.combinatorics/)

* [Compatibility Test Matrix](http://build.clojure.org/job/math.combinatorics-test-matrix/)

Changelog
========================================
* Release 0.1.4 om 2017-01-06
  * Support for clojurescript (tested with 1.9.293)
  * Dropped support for Clojure 1.2 - 1.6

* Release 0.1.3 on 2016-06-02
  * Changed boxing to use Long/valueOf.

* Release 0.1.2 on 2016-05-18
  * Added explicit boxing to eliminate auto-boxing warnings.
    No change in functionality or performance from previous release.

* Release 0.1.1 on 2015-03-20
  * Backwards compatibility with 1.2.0 and 1.2.1

* Release 0.1.0 on 2015-03-17
  * combinations and subsets now have special handling for duplicate items
  * Added count-permutations, count-combinations, count-subsets,
     nth-permutation, nth-combination, nth-subset
     drop-permutations, permutation-index

* Release 0.0.9 on 2015-03-16
  * Exclude "update" function from core for compatibility with 1.7.

* Release 0.0.8 on 2014-07-20
  * Minor improvement of helper function used by permutations.

* 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.

* Release 0.0.5 on 2013-10-31
  * Added partitions function

* Release 0.0.4 on 2013-03-26
  * Moved comment after ns declaration

* Release 0.0.3 on 2012-07-06
  * Fixed bug with (selections [false] 3) returning nil
  * Fixed test syntax for Clojure 1.4.0/1.5.0

* Release 0.0.2 on 2011-10-24
  * Deprecated lex-permutations (permutations is now intelligent)

* Release 0.0.1 on 2011-09-29
  * Initial release.
  * Source-compatible with clojure.contrib.math, except for the name change.

License
========================================

Distributed under the Eclipse Public License, the same as Clojure.