code-reviews

martinklepsch 2015-11-06T11:58:55.000002Z

I made a custom Clojure type! https://github.com/martinklepsch/custom-comparator-set First time I’ve done this. It’s pretty little code easy to review I guess. Comments on style/indentation/thinking/everything most welcome :simple_smile:

martinklepsch 2015-11-06T15:23:22.000004Z

Here’s v2: https://github.com/martinklepsch/custom-comparator-set :simple_smile:

xeqi 2015-11-06T16:20:51.000006Z

@martinklepsch: do you need to define .equals and .hashcode?

martinklepsch 2015-11-06T16:21:18.000007Z

@xeqi: it probably would make sense :simple_smile:

martinklepsch 2015-11-06T16:21:29.000008Z

I’m just doing that for the cljs side of things

xeqi 2015-11-06T16:21:39.000009Z

I imagine a test case would be trying to use the sets as keys in a {}

xeqi 2015-11-06T16:22:38.000010Z

also, tried to build locally to test and got "Could not find artifact adzerk:boot-test:jar:1.0.5-SNAPSHOT"

martinklepsch 2015-11-06T16:23:10.000012Z

Yeah, that’s a local mod for cljc testing. PR outstanding in the boot-test repo.

martinklepsch 2015-11-06T16:23:19.000013Z

Sorry about that

martinklepsch 2015-11-06T16:23:48.000015Z

Thanks for the test case idea, was actually wondering how I can test this :simple_smile:

martinklepsch 2015-11-06T16:24:02.000016Z

how’s your clojure/java post coming along btw :simple_smile:

xeqi 2015-11-06T16:24:32.000017Z

I'm not 100% on that being a good testcase fwiw

xeqi 2015-11-06T16:24:56.000018Z

haha, it will have to be several. That much stuff could easily be an ebook

ghadi 2015-11-06T16:33:38.000019Z

martinklepsch: for the print-method do doseq on the vals with print-method recursively

martinklepsch 2015-11-06T16:34:17.000020Z

@xeqi: is this what you had in mind kind of?

(deftest hashing
  (let [cset (ccset/custom-comparator-set :k {:k "a"})
        kmap (-> {} (assoc cset 1) (assoc cset 2))]
    (t/is (= 2 (get kmap cset)))
    (t/is (= 1 (count kmap)))))

martinklepsch 2015-11-06T16:35:54.000021Z

ignore this

martinklepsch 2015-11-06T16:38:42.000022Z

(deftest hashing
  (let [cset1 (ccset/custom-comparator-set :k {:k "a"})
        cset2 (ccset/custom-comparator-set :k {:k "a"})
        kmap (-> {} (assoc cset1 1) (assoc cset2 2))]
    (t/is (= 2 (get kmap cset1)))
    (t/is (= 2 (get kmap cset2)))
    (t/is (= 1 (count kmap)))))
this is better

martinklepsch 2015-11-06T16:39:43.000024Z

@ghadi: thanks, do you mean like this?

(defmethod print-method CustomComparatorSet [v ^java.io.Writer w]
  (.write w "#CustomComparatorSet{")
  (doseq [x (seq v)]
    (print-method x w)
    (.write w " "))
  (.write w "}"))

1👍
martinklepsch 2015-11-06T16:40:46.000025Z

would I use interpose to add spaces between items?

ghadi 2015-11-06T16:41:00.000026Z

no

ghadi 2015-11-06T16:41:07.000027Z

this is good

ghadi 2015-11-06T16:41:15.000028Z

you're doing imperative printing

ghadi 2015-11-06T16:41:27.000029Z

keep it clear

martinklepsch 2015-11-06T16:46:51.000030Z

@ghadi: if I want to elide the trailing space before the closing } I’d use loop and check for rest?

xeqi 2015-11-06T16:53:57.000031Z

@martinklepsch yeah, something like that to see if you needed to override those methods. Did the test fail without custom .hashcode/.equals?

martinklepsch 2015-11-06T16:54:12.000032Z

@xeqi: yes they’re failing

martinklepsch 2015-11-06T16:54:46.000033Z

@xeqi: is there some place where I can lookup protocols/interfaces for clojure types?

martinklepsch 2015-11-06T16:55:36.000034Z

like how do I figure out what Interface I have to implement for .hashcode and .equals?

xeqi 2015-11-06T16:55:51.000035Z

@martinklepsch https://github.com/ztellman/collection-check is where I would start

xeqi 2015-11-06T16:56:24.000037Z

Those two are on Object

martinklepsch 2015-11-06T17:05:22.000038Z

@xeqi: where you would start with testing?

xeqi 2015-11-06T17:25:24.000039Z

@martinklepsch I meant for finding protocols/interfaces for Clojure types. For finding out what classes/interfaces those methods belog to higher up, I don't know a good place

martinklepsch 2015-11-06T17:26:04.000040Z

ok. thanks! :simple_smile:

martinklepsch 2015-11-06T17:26:14.000041Z

in the process of adding collection-check to the tests

xeqi 2015-11-06T17:27:03.000042Z

Though if I was testing your set, i'd highly consider throwing collection-check at (comparable-set identity) compared to #{}

martinklepsch 2015-11-06T17:27:37.000043Z

that’s exactly what I’m doing :simple_smile:

martinklepsch 2015-11-06T17:28:05.000044Z

@xeqi: do you think comparable-set is a better name (just asking because you used it)

xeqi 2015-11-06T17:29:09.000045Z

Though that might not be quite right, cause is (= (c-s identity) (c-s #(%1))? How does the comparable effect equality? Etc etc

xeqi 2015-11-06T17:30:02.000046Z

I hadn't though about it, just typed that cause I'm on mobile atm and didn't want to go back to the code to find real name

xeqi 2015-11-06T17:34:49.000047Z

I don't have a good term for this concept

xeqi 2015-11-06T17:49:40.000048Z

@martinklepsch back to actual review, could you delegate .contains to (contains? data (comparator v))? I think that would avoid building a set and walking every element each time.

martinklepsch 2015-11-06T17:50:48.000049Z

@xeqi: good suggestion thanks!