Skip to content

ComputeSoftware/sorted-multiset

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sorted-multiset

Persistent sorted Multiset with multiplicity capped at 1 and Set value equivalence.

Use Case

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.

Release Information

com.computesoftware/sorted-multiset {:mvn/version "0.1.6"}

Example usage

(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"})

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published