dtype-next icon indicating copy to clipboard operation
dtype-next copied to clipboard

universal comparator

Open genmeblog opened this issue 1 year ago • 3 comments

After fixing #99 some regression was introduced in tablecloth regarding comparison of any type. Till the last version, the following code was working. < or > could be treated as an universal comparator.

(tech.v3.datatype.argops/argsort tech.v3.datatype.functional/< ["ab" "bc"])
;; => [0 1]

Is it possible to make a comparator which acts universally on every type or should I reach for Clojure comparator?

genmeblog avatar Jun 25 '24 08:06 genmeblog

https://clojurians.zulipchat.com/#narrow/stream/236259-tech.2Eml.2Edataset.2Edev/topic/sorting.20string.20-.20regression.20in.207.2E030

genmeblog avatar Jun 25 '24 08:06 genmeblog

I think using clojure.core/compare as stated in the documentation makes sense to me. Here as some odd facts about clojure.core/compare --

user> (instance? java.util.Comparator clojure.core/compare)
true
user> 

Yet it is defined just like any other function in core... -- all AFunction's are comparators.

If you need to reverse the comparator then you can with a Comparator interface default function:


user> (require '[tech.v3.datatype.argops :as argops])
nil
user> (require '[tech.v3.datatype :as dt])
nil
user> (argops/argsort (.reversed clojure.core/compare) ["ab" "bc"])
[1 0]

So I don't think I want to add a dtype/functional wrapper for this as it seems reasonable to me.

cnuernber avatar Sep 12 '24 15:09 cnuernber

I guess if you knew the datatype of the incoming thing then you could choose a specific comparator to use and default to the code in the compare data.

cnuernber avatar Sep 12 '24 15:09 cnuernber