contains? returns false for existing set item.
dimovich opened this issue · comments
dimovich commented
(require '[me.tonsky.persistent-sorted-set :as pset])
(defn update-index [index-data all-data syms]
(let [cmp-fn (fn [sym1 sym2]
(compare (get-in all-data [sym1 :index])
(get-in all-data [sym2 :index])))]
(->> syms
(reduce (fn [index-data sym]
(-> index-data
(pset/disj sym cmp-fn)
(pset/conj sym cmp-fn)))
(or (not-empty index-data) (pset/sorted-set-by cmp-fn))))))
(def data1 {"Sasha" {:index 0}})
(def data2 {"Sasha" {:index 0}
"Misha" {:index 436},
"Vanea" {:index 451}})
(def index (-> (update-index nil data1 ["Sasha"])
(update-index data2 ["Misha" "Vanea"])))
;; => #{"Sasha" "Misha" "Vanea}
(contains? index "Sasha"))
;; => false
contains?
returns false
, even though index
has item "Sasha"
.
Nikita Prokopov commented
I guess conj/disj with compare are confusing. They allow you to use different compare function only for the duration of single operation, but do not change the compare function that set itself uses by default. Set will always use the one specified on creation. In your case it’s a cmp-fn closed over data1
dimovich commented
@tonsky Thank you for clarifying. Is it possible to update the cmp-fn specified on creation? Or maybe have option to specify the cmp-fn when calling contains?
.
Nikita Prokopov commented
that would require re-sorting the set. So just create a new one