Persistent sorted Multiset with multiplicity capped at 1 and Set value equivalence.
Default Java TreeSet semantics
define elements that are deemed equal by compareTo as equal from the standpoint of the set. In some cases, it is
useful to allow elements deemed equal by compareTo to reside in the set.
com.computesoftware/sorted-multiset {:mvn/version "0.1.6"}(require '[com.computesoftware.sorted-multiset :as sm])
(sm/sorted-multiset-by #(compare (:k %1) (:k %2))
{:k 0
:v "a"}
{:k 1
:v "b"}
{:k 0
:v "c"})
=> #{{:k 0, :v "a"} {:k 0, :v "c"} {:k 1, :v "b"}}This library has differing equality semantics that Java Treeset. Clojure's subseq
and rsubseq assume Java Treeset equality semantics
, which this library does not follow. As a result, both of those functions do not always return correct results.
Therefore, this library provides its own subseq and rsubseq functions that will work as expected.
(def sm
(sorted-multiset-by #(compare (:k %1) (:k %2))
{:k 0
:v "a"}
{:k 1
:v "b"}
{:k 1
:v "c"}
{:k 0
:v "c"}))
=> #'com.computesoftware.sorted-multiset/sm
;; INCORRECT: The first element should not have been included.
(subseq sm > {:k 0})
=> ({:k 0, :v "c"} {:k 1, :v "b"} {:k 1, :v "c"})
;; CORRECT: We get the expected result using sorted-multiset's subseq.
(sm/subseq sm > {:k 0})
=> ({:k 1, :v "b"} {:k 1, :v "c"})