Details

Type: Defect

Status: Closed

Priority: Major

Resolution: Declined

Affects Version/s: Release 1.7

Fix Version/s: None

Component/s: None

Labels:

Environment:Windows 8.1
Description
Function generates a sequence of prime numbers. Uses a priority queue implemented as a sorted set of vectors [priority val].
On the iteration (x = 10) fails with message:
(defn sieve [[x & t] pq] (lazyseq (if (or (empty? pq) (< x (ffirst pq))) (cons x (sieve t (conj pq [(* x x) (next (iterate (partial + x) (* x x)))]))) (sieve t (loop [pq pq] (let [[key val :as head] (first pq)] (if (= x key) (recur (conj (disj pq head) [(first val) (next val)])) pq) )))))) (def primes (sieve (iterate inc 2) (sortedset))) (take 4 primes) ;; => (2 3 5 7) (take 5 primes) ClassCastException clojure.lang.Iterate cannot be cast to java.lang.Comparable clojure.lang.Util.compare (Util.java:153)
When x = 10 it goes to the (loop ...) section and fails while trying to recur.
Sets are unordered (not sequential) and cannot be used with sequential destructuring. You can wrap (seq ) around the conj in the recur if you wish to make it sequential.