Created
September 3, 2011 12:16
-
-
Save Netpilgrim/1191098 to your computer and use it in GitHub Desktop.
Problem with -> (solved)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defprotocol Graph | |
(set-vertex [this v]) | |
(set-edge [this from to] | |
[this from to weight])) | |
(defrecord DirectedGraph [vs em wm] | |
;; A directed graph: vs is a sorted set of vertices, em is a map from vertices | |
;; to a sorted set of adjacent vertices, wm is a map from edges given as | |
;; vertex tuple [from to] to weights. | |
Graph | |
(set-vertex [_ v] | |
(DirectedGraph. (conj vs v) | |
(conj em [v (sorted-set)]) | |
wm)) | |
(set-edge [this from to] | |
(set-edge this from to nil)) | |
(set-edge [_ from to weight] | |
(DirectedGraph. vs | |
(update-in em [from] conj to) | |
(conj wm [[from to] weight])))) | |
(def test-graph (-> (DirectedGraph. (sorted-set) {} {}) | |
(set-vertex :q) | |
(set-vertex :a) | |
(set-vertex :b) | |
(set-vertex :c) | |
(set-vertex :d) | |
(set-vertex :s) | |
(set-edge :q :a 8) | |
(set-edge :q :c 3) | |
(set-edge :a :b 2) | |
(set-edge :a :c 2) | |
(set-edge :a :d 1) | |
(set-edge :b :c 2) | |
(set-edge :b :s 1) | |
(set-edge :c :d 8) | |
(set-edge :d :s 7))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment