Skip to content

Commit

Permalink
Fix #212: implement equality for seq nodes (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Jan 30, 2023
1 parent a9fcdc3 commit cbf82fd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ https://github.com/clj-commons/rewrite-clj/issues/189[#189]
* exceptions thrown while reading now include `:row` and `:col` keys in `ex-data` https://github.com/clj-commons/rewrite-clj/pull/181[#181] (thanks @ferdinand-beyer)
* docs
** a docstring typo fix https://github.com/clj-commons/rewrite-clj/pull/191[#191] (thanks @BTowersCoding!)

* Implement equality for seq nodes https://github.com/clj-commons/rewrite-clj/issues/212[#212] (@borkdude)

=== v1.1.45

Expand Down
20 changes: 12 additions & 8 deletions src/rewrite_clj/node/seq.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@

;; ## Constructors

(defn list-node
"Create a node representing a list with `children`.
(let [;; re-use seq-fn for all instances so equals works
list-node-seq-fn #(apply list %)]
(defn list-node
"Create a node representing a list with `children`.
```Clojure
(require '[rewrite-clj.node :as n])
Expand All @@ -53,8 +55,8 @@
n/string)
;; => \"(1 2 3)\"
```"
[children]
(->SeqNode :list "(%s)" 2 #(apply list %) children))
[children]
(->SeqNode :list "(%s)" 2 list-node-seq-fn children)))

(defn vector-node
"Create a node representing a vector with `children`.
Expand Down Expand Up @@ -101,8 +103,10 @@
[children]
(->SeqNode :set "#{%s}" 3 set children))

(defn map-node
"Create a node representing a map with `children`.
(let [;; re-use seq-fn for all instances for equality
map-seq-fn #(apply hash-map %)]
(defn map-node
"Create a node representing a map with `children`.
```Clojure
(require '[rewrite-clj.node :as n])
Expand Down Expand Up @@ -138,5 +142,5 @@
;; => \"{:a 1 :a 2}\"
```
See [docs on maps with duplicate keys](/doc/01-user-guide.adoc#maps-with-duplicate-keys)."
[children]
(->SeqNode :map "{%s}" 2 #(apply hash-map %) children))
[children]
(->SeqNode :map "{%s}" 2 map-seq-fn children)))
10 changes: 10 additions & 0 deletions test/rewrite_clj/node/equals_test.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(ns rewrite-clj.node.equals-test
(:require
[clojure.test :refer [deftest is]]
[rewrite-clj.node :as node]))

(deftest equals-test
(is (= (node/list-node []) (node/list-node [])))
(is (= (node/vector-node []) (node/vector-node [])))
(is (= (node/set-node []) (node/set-node [])))
(is (= (node/map-node []) (node/map-node []))))

0 comments on commit cbf82fd

Please sign in to comment.