Skip to content

Commit

Permalink
Implement IHashEq
Browse files Browse the repository at this point in the history
Test clj correctness with ztellman collection-check
Will have to eventually migrate to a common ProxyMap and ProxySet for
both clj and cljs and just override the necessary methods (like in
potemkin).
Hash codes and seq needs to be cached for performance
  • Loading branch information
frankiesardo committed Oct 13, 2015
1 parent 0c8de72 commit 828c18a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
4 changes: 3 additions & 1 deletion build.boot
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
:resource-paths #{"src"}
:dependencies '[[adzerk/boot-cljs "0.0-3308-0" :scope "test"]
[adzerk/boot-cljs-repl "0.1.9" :scope "test"]
[crisptrutski/boot-cljs-test "0.1.0-SNAPSHOT" :scope "test"]])
[crisptrutski/boot-cljs-test "0.1.0-SNAPSHOT" :scope "test"]
[collection-check "0.1.6" :scope "test"
:exclusions [org.clojure/clojure]]])

(require
'[adzerk.boot-cljs :refer [cljs]]
Expand Down
8 changes: 6 additions & 2 deletions src/linked/map.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Counted
IObj
IFn
IHashEq
ILookup
IPersistentCollection
IPersistentVector
Expand Down Expand Up @@ -118,13 +119,16 @@

;; IEditableCollection

IHashEq
(hasheq [this] (.hasheq (into {} this)))

Object
(toString [this]
(str "{" (string/join ", " (for [[k v] this] (str k " " v))) "}"))
(equals [this other]
(.equiv this other))
(hashCode [this]
(hash (into {} this)))]
(.hashCode (into {} this)))]
:cljs
[Object
(toString [coll]
Expand Down Expand Up @@ -163,7 +167,7 @@
(-equiv [coll other] (equiv-map coll other))

IHash
(-hash [coll] (hash-unordered-coll coll))
(-hash [coll] (hash (into {} coll)))

ISequential

Expand Down
8 changes: 6 additions & 2 deletions src/linked/set.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#?(:clj (:import (clojure.lang Counted
IObj
IFn
IHashEq
ILookup
IPersistentCollection
IPersistentSet
Expand Down Expand Up @@ -70,11 +71,14 @@
(withMeta [this m]
(LinkedSet. (.withMeta ^IObj linked-map m)))

IHashEq
(hasheq [this] (.hasheq (into #{} this)))

Object
(toString [this]
(str "[" (string/join " " (map str this)) "]"))
(hashCode [this]
(reduce + (map hash (.seq this))))
(.hashCode (into #{} this)))
(equals [this other]
(.equiv this other))]
:cljs
Expand Down Expand Up @@ -109,7 +113,7 @@
other)))

IHash
(-hash [coll] (hash-unordered-coll coll))
(-hash [coll] (hash (into #{} coll)))

ISeqable
(-seq [coll] (when-let [s (seq linked-map)] (map key s)))
Expand Down
10 changes: 8 additions & 2 deletions test/linked/map_test.cljc
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
(ns linked.map-test
(:require [linked.core :as linked]
#?(:clj [clojure.test :refer :all]
:cljs [cljs.test :refer-macros [is are testing deftest run-tests]])
#?@(:clj [[clojure.test :refer :all]
[collection-check :refer :all]
[clojure.test.check.generators :as gen]]
:cljs [[cljs.test :refer-macros [is are testing deftest run-tests]]])
#?(:cljs [cljs.reader :refer [read-string]])))

#?(:clj
(deftest check
(assert-map-like (linked/map) gen/int gen/int)))

#?(:clj
(deftest implementations
(let [basic (linked/map)]
Expand Down
11 changes: 9 additions & 2 deletions test/linked/set_test.cljc
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
(ns linked.set-test
(:require [linked.core :as linked]
#?(:clj [clojure.test :refer :all]
:cljs [cljs.test :refer-macros [is are testing deftest run-tests]])
#?@(:clj [[clojure.test :refer :all]
[collection-check :refer :all]
[clojure.test.check.generators :as gen]]
:cljs [[cljs.test :refer-macros [is are testing deftest run-tests]]])
#?(:cljs [cljs.reader :refer [read-string]])))


#?(:clj
(deftest check
(assert-set-like (linked/set) gen/int)))

#?(:clj
(deftest implementations
(let [s (linked/set)]
Expand Down

0 comments on commit 828c18a

Please sign in to comment.