Skip to content

Commit ff5c70e

Browse files
committed
Prepare cljc files
1 parent e551f9c commit ff5c70e

File tree

3 files changed

+32
-39
lines changed

3 files changed

+32
-39
lines changed

README.md

Lines changed: 6 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.1.3
12+
Latest stable release: 0.1.4
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.1.3
1818
[Leiningen](https://github.com/technomancy/leiningen) dependency information:
1919

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

2424
[Maven](http://maven.apache.org/) dependency information:
@@ -159,9 +159,12 @@ Developer Information
159159

160160
Changelog
161161
========================================
162+
* Release 0.1.4 om 2017-01-06
163+
* Support for clojurescript
164+
162165
* Release 0.1.3 on 2016-06-02
163166
* Changed boxing to use Long/valueOf.
164-
167+
165168
* Release 0.1.2 on 2016-05-18
166169
* Added explicit boxing to eliminate auto-boxing warnings.
167170
No change in functionality or performance from previous release.

src/main/clojure/clojure/math/combinatorics.clj renamed to src/main/clojure/clojure/math/combinatorics.cljc

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

44
;; by Mark Engelberg (mark.engelberg@gmail.com)
5-
;; Last updated - May 18, 2016
5+
;; Last updated - Jan 6, 2017
66

77
(ns
88
#^{:author "Mark Engelberg",
99
:doc "Efficient, functional algorithms for generating lazy
1010
sequences for common combinatorial functions. (See the source code
1111
for a longer description.)"}
1212
clojure.math.combinatorics
13-
(:refer-clojure :exclude [update]))
13+
(:refer-clojure :exclude [update]))
1414

1515
(comment
1616
"
@@ -88,17 +88,6 @@ to write our own version that considers the empty-list to be distinct"
8888
(apply distinct? s)
8989
true))
9090

91-
(defmacro assert-with-message
92-
"Clojure 1.2 didn't allow asserts with a message, so we roll our own here for backwards compatibility"
93-
[x message]
94-
(when *assert*
95-
`(when-not ~x
96-
(throw (new AssertionError (str "Assert failed: " ~message "\n" (pr-str '~x)))))))
97-
98-
;; so this code works with both 1.2.x and 1.3.0:
99-
(def ^{:private true} plus (first [+' +]))
100-
(def ^{:private true} mult (first [*' *]))
101-
10291
(defn- index-combinations
10392
[n cnt]
10493
(lazy-seq
@@ -310,7 +299,7 @@ In prior versions of the combinatorics library, there were two similar functions
310299
{:pre [(integer? n) (not (neg? n))]}
311300
; (Long/valueOf (long 1)) eliminates loop auto-boxing warning, in a way compatible with Clojure 1.2
312301
(loop [acc (Long/valueOf (long 1)), n n]
313-
(if (zero? n) acc (recur (mult acc n) (dec n)))))
302+
(if (zero? n) acc (recur (*' acc n) (dec n)))))
314303

315304
(defn- factorial-numbers
316305
"Input is a non-negative base 10 integer, output is the number in the
@@ -333,9 +322,8 @@ expressed as a list of 'digits'"
333322
"Input should be a sorted sequential collection l of distinct items,
334323
output is nth-permutation (0-based)"
335324
[l n]
336-
(assert-with-message (< n (factorial (count l)))
337-
(format "%s is too large. Input has only %s permutations."
338-
(str n) (str (factorial (count l)))))
325+
(assert (< n (factorial (count l)))
326+
(print-str n "is too large. Input has only" (factorial (count l)) "permutations."))
339327
(let [length (count l)
340328
fact-nums (factorial-numbers n)]
341329
(loop [indices (concat (repeat (- length (count fact-nums)) 0)
@@ -413,10 +401,9 @@ Output is a list of 'digits' in this wacky duplicate factorial number system"
413401
(defn- nth-permutation-duplicates
414402
"Input should be a sorted sequential collection l of distinct items,
415403
output is nth-permutation (0-based)"
416-
[l n]
417-
(assert-with-message (< n (count-permutations l))
418-
(format "%s is too large. Input has only %s permutations."
419-
(str n) (str (count-permutations l))))
404+
[l n]
405+
(assert (< n (count-permutations l))
406+
(print-str n "is too large. Input has only" (count-permutations l) "permutations."))
420407
(loop [freqs (into (sorted-map) (frequencies l)),
421408
indices (factorial-numbers-with-duplicates n freqs)
422409
perm []]
@@ -480,8 +467,8 @@ output is nth-permutation (0-based)"
480467
(zero? k) 1
481468
(= k 1) n
482469
(> k (quot n 2)) (recur n (- n k))
483-
:else (/ (apply mult (range (inc (- n k)) (inc n)))
484-
(apply mult (range 1 (inc k))))))
470+
:else (/ (apply *' (range (inc (- n k)) (inc n)))
471+
(apply *' (range 1 (inc k))))))
485472

486473
(defn- ^{:dynamic true} count-combinations-from-frequencies [freqs t]
487474
(let [counts (vals freqs)
@@ -495,7 +482,7 @@ output is nth-permutation (0-based)"
495482
(= (count freqs) 1) 1
496483
:else
497484
(let [new-freqs (dec-key freqs (first (keys freqs)))]
498-
(plus (count-combinations-from-frequencies new-freqs (dec t))
485+
(+' (count-combinations-from-frequencies new-freqs (dec t))
499486
(count-combinations-from-frequencies (dissoc freqs (first (keys freqs))) t))))))
500487

501488
(defn- count-combinations-unmemoized
@@ -516,16 +503,16 @@ so that we can memoize over a series of calls."
516503
(loop [n pow, y (Long/valueOf (long 1)), z base]
517504
(let [t (even? n), n (quot n 2)]
518505
(cond
519-
t (recur n y (mult z z))
520-
(zero? n) (mult z y)
521-
:else (recur n (mult z y) (mult z z))))))
506+
t (recur n y (*' z z))
507+
(zero? n) (*' z y)
508+
:else (recur n (*' z y) (*' z z))))))
522509

523510
(defn- count-subsets-unmemoized
524511
[items]
525512
(cond
526513
(empty? items) 1
527514
(all-different? items) (expt-int 2 (count items))
528-
:else (apply plus (for [i (range 0 (inc (count items)))]
515+
:else (apply +' (for [i (range 0 (inc (count items)))]
529516
(count-combinations-unmemoized items i)))))
530517

531518
(defn count-subsets
@@ -569,9 +556,9 @@ represented by freqs"
569556
(defn nth-combination
570557
"The nth element of the sequence of t-combinations of items"
571558
[items t n]
572-
(assert-with-message (< n (count-combinations items t))
573-
(format "%s is too large. Input has only %s combinations."
574-
(str n) (str (count-combinations-unmemoized items t))))
559+
(assert (< n (count-combinations items t))
560+
(print-str n "is too large. Input has only"
561+
(count-combinations-unmemoized items t) "combinations."))
575562
(if (all-different? items)
576563
(nth-combination-distinct items t n)
577564
(binding [count-combinations-from-frequencies (memoize count-combinations-from-frequencies)]
@@ -585,9 +572,8 @@ represented by freqs"
585572

586573
(defn nth-subset
587574
[items n]
588-
(assert-with-message (< n (count-subsets items))
589-
(format "%s is too large. Input has only %s subsets."
590-
(str n) (str (count-subsets items))))
575+
(assert (< n (count-subsets items))
576+
(print-str n "is too large. Input has only" (count-subsets items) "subsets."))
591577
(loop [size 0,
592578
n n]
593579
(let [num-combinations (count-combinations items size)]

src/test/clojure/clojure/math/test_combinatorics.clj renamed to src/test/clojure/clojure/math/test_combinatorics.cljc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
(ns clojure.math.test-combinatorics
2-
(:use clojure.test
3-
clojure.math.combinatorics))
2+
(:require [clojure.math.combinatorics :refer
3+
[subsets combinations selections permutations cartesian-product
4+
partitions nth-permutation permutation-index drop-permutations
5+
count-permutations count-combinations count-subsets
6+
nth-subset nth-combination]]
7+
[clojure.test :refer [deftest is are testing run-tests]]))
48

59
(deftest test-combinations
610
(are [x y] (= x y)

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