Skip to content

Commit 47ac63a

Browse files
committed
Improved compatibility with Clojure 1.4 and lower
Was running into problem in earlier versions where memoize was crashing because it was receiving inputs that weren't comparable.
1 parent 59429f2 commit 47ac63a

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ output is nth-permutation (0-based)"
489489
(defn- count-combinations-unmemoized
490490
"We need an internal version that doesn't memoize each call to count-combinations-from-frequencies
491491
so that we can memoize over a series of calls."
492-
[items t]
492+
[items t]
493493
(if (all-different? items)
494494
(n-take-k (count items) t)
495495
(count-combinations-from-frequencies (frequencies items) t)))
@@ -557,12 +557,12 @@ represented by freqs"
557557
(defn nth-combination
558558
"The nth element of the sequence of t-combinations of items"
559559
[items t n]
560-
(binding [count-combinations-from-frequencies (memoize count-combinations-from-frequencies)]
561-
(assert (< n (count-combinations-unmemoized items t))
562-
(format "%s is too large. Input has only %s combinations."
563-
(str n) (str (count-combinations-unmemoized items t))))
564-
(if (all-different? items)
565-
(nth-combination-distinct items t n)
560+
(assert (< n (count-combinations items t))
561+
(format "%s is too large. Input has only %s combinations."
562+
(str n) (str (count-combinations-unmemoized items t))))
563+
(if (all-different? items)
564+
(nth-combination-distinct items t n)
565+
(binding [count-combinations-from-frequencies (memoize count-combinations-from-frequencies)]
566566
(let [v (vec (distinct items))
567567
f (frequencies items),
568568
indices (apply concat
@@ -573,10 +573,10 @@ represented by freqs"
573573

574574
(defn nth-subset
575575
[items n]
576+
(assert (< n (count-subsets items))
577+
(format "%s is too large. Input has only %s subsets."
578+
(str n) (str (count-subsets items))))
576579
(binding [count-combinations-from-frequencies (memoize count-combinations-from-frequencies)]
577-
(assert (< n (count-subsets items))
578-
(format "%s is too large. Input has only %s subsets."
579-
(str n) (str (count-subsets-unmemoized items))))
580580
(loop [size 0,
581581
n n]
582582
(let [num-combinations (count-combinations-unmemoized items size)]

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