diff --git a/src/graffy/core.clj b/src/graffy/core.clj index 85089bc..3d4e65c 100644 --- a/src/graffy/core.clj +++ b/src/graffy/core.clj @@ -1,17 +1,17 @@ (ns graffy.core) (defn add-edge [g s d] - (when-not (@g s) (swap! g assoc s (sorted-set))) - (when-not (@g d) (swap! g assoc d (sorted-set))) - (swap! g update-in [s] conj d) - (swap! g update-in [d] conj s) - g) + (-> g + (assoc s (or (g s) (sorted-set))) + (assoc d (or (g d) (sorted-set))) + (update-in [s] conj d) + (update-in [d] conj s))) (defn vertices [g] - (keys @g)) + (keys g)) (defn neighbors [g n] - (or (@g n) + (or (g n) (sorted-set))) (defn edges [g n] @@ -45,5 +45,3 @@ (if (contains? (into #{} acc) nxt) (recur acc rst) (recur (conj acc nxt) (apply list (concat rst (neighbor-list g nxt))))))))) ;; ** here - - diff --git a/test/graffy/core_test.clj b/test/graffy/core_test.clj index 6c80695..f57b81c 100644 --- a/test/graffy/core_test.clj +++ b/test/graffy/core_test.clj @@ -4,38 +4,36 @@ (deftest test-vertices (testing "vertices" - (let [g (atom {})] + (let [g {}] (is (= #{} (into #{} (vertices g)))) - (add-edge g :a :b) - (is (= #{:a :b} (into #{} (vertices g))))))) + (is (= #{:a :b} (into #{} (vertices (add-edge g :a :b)))))))) (deftest test-neighbors (testing "neighbors" - (let [g (atom {})] - (add-edge g :a :b) + (let [g (add-edge {} :a :b)] (is (= #{:b} (into #{} (neighbors g :a)))) (is (= #{:a} (into #{} (neighbors g :b)))) (is (= #{} (into #{} (neighbors g :c))))))) (deftest test-edges (testing "edges" - (let [g (atom {})] - (-> g (add-edge :a :b) - (add-edge :a :c) - (add-edge :a :d) - (add-edge :b :d)) + (let [g (-> {} + (add-edge :a :b) + (add-edge :a :c) + (add-edge :a :d) + (add-edge :b :d))] (is (= #{[:a :b] [:a :c] [:a :d]} (into #{} (edges g :a)))) (is (= #{[:d :a] [:d :b]} (into #{} (edges g :d))))))) (deftest test-all-edges (testing "all-edges" - (let [g (-> (atom {}) + (let [g (-> {} (add-edge :a :b))] (is (= #{[:a :b] [:b :a]} (into #{} (all-edges g))))))) (deftest test-traversal (testing "depth-first, breadth-first traversal" - (let [g (-> (atom {}) + (let [g (-> {} (add-edge 0 1) (add-edge 0 2) (add-edge 0 5)