Skip to content

Commit

Permalink
First go at translating to cljs
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Chadwick committed Oct 31, 2015
1 parent ab4f5bb commit 4ded279
Show file tree
Hide file tree
Showing 23 changed files with 29 additions and 21 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .travis.yml
100644 → 100755
Empty file.
Empty file modified CHANGELOG.md
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified doc/loom_logo.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified doc/loom_logo.svg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified doc/ns-dep-graph.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified project.clj
100644 → 100755
Empty file.
7 changes: 4 additions & 3 deletions src/loom/alg.cljc
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ can use these functions."
[g]
(letfn [(color-component [coloring start]
(loop [coloring (assoc coloring start 1)
queue (conj #?(:clj clojure.lang.PersistentQueue/EMPTY
:cljs cljs.core.PersistentQueue/EMPTY) start)]
queue (conj #?(:clj clojure.lang.PersistentQueue/EMPTY
:cljs (.-EMPTY cljs.core/PersistentQueue)) start)]

This comment has been minimized.

Copy link
@danielcompton

danielcompton Oct 31, 2015

Contributor

This can use #queue [] in CLJS.

(if (empty? queue)
coloring
(let [v (peek queue)
Expand Down Expand Up @@ -606,7 +606,8 @@ can use these functions."
([g src target heur q explored]
(cond
;; queue empty, target not reachable
(empty? q) (throw (Exception. "Target not reachable from source"))
(empty? q) (throw #?(:clj (Exception. "Target not reachable from source")
:cljs (js/Error. "Target not reachable from source")))
;; target found, build path and return
(= (first (peek q)) target) (let [u (first (peek q))
parent ((second (peek q)) 1)
Expand Down
Empty file modified src/loom/alg_generic.cljc
100644 → 100755
Empty file.
Empty file modified src/loom/attr.cljc
100644 → 100755
Empty file.
Empty file modified src/loom/dataflow.clj
100644 → 100755
Empty file.
Empty file modified src/loom/flow.cljc
100644 → 100755
Empty file.
Empty file modified src/loom/gen.cljc
100644 → 100755
Empty file.
43 changes: 25 additions & 18 deletions src/loom/graph.cljc
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on adjacency lists."
loom.graph
(:require [loom.alg-generic :refer [bf-traverse]]))

;;;
;;; Protocols
;;;

(defprotocol Graph
(nodes [g] "Returns a collection of the nodes in graph g")
Expand Down Expand Up @@ -82,7 +85,7 @@ on adjacency lists."

(def ^{:dynamic true
:doc "Weight used when none is given for edges in weighted graphs"}
*default-weight* 1)
*default-weight* 1)

(def default-graph-impls
{:all
Expand Down Expand Up @@ -153,22 +156,6 @@ on adjacency lists."
(apply dissoc m nodes)
adjacents))

(comment (defmacro extended-record [rn fv & spec]
(concat `(defrecord ~rn ~fv)
(mapcat
(fn [[pn pm]]
;(println pm)
(cons pn
(mapcat
(fn [[fk fv]]
;(println " " pn " " `~(cons (symbol (name fk)) (rest fv)))
(if (vector? (second fv))
(list `~(cons (symbol (name fk)) (rest fv)))
(map (fn [fa] `~(cons (symbol (name fk)) fa)) (rest fv)))
)
(if (map? pm) pm (eval pm)))))
(partition 2 spec)))))

;;;
;;; Records for basic graphs -- one edge per vertex pair/direction,
;;; loops allowed
Expand All @@ -177,6 +164,27 @@ on adjacency lists."
;; TODO: preserve metadata?
;; TODO: leverage zippers for faster record updates?


; hi, as Clojurescript doesn't have extend,
; I used this macro to convert the
; following code to Clojurescript-compatable code.
; It's not pretty but it seems to work. Oh and the macro
; only runs in Clojure

(comment (defmacro extended-record [rn fv & spec]
(concat `(defrecord ~rn ~fv)
(mapcat
(fn [[pn pm]]
(cons pn
(mapcat
(fn [[fk fv]]
(if (vector? (second fv))
(list `~(cons (symbol (name fk)) (rest fv)))
(map (fn [fa] `~(cons (symbol (name fk)) fa)) (rest fv)))
)
(if (map? pm) pm (eval pm)))))
(partition 2 spec)))))

(comment
(defrecord BasicEditableGraph [nodeset adj])
(defrecord BasicEditableDigraph [nodeset adj in])
Expand All @@ -185,7 +193,6 @@ on adjacency lists."

(defrecord BasicEditableGraph [nodeset adj] Graph (nodes [g] (:nodeset g)) (edges [g] (for [n1 (nodes g) e (out-edges g n1)] e)) (has-node? [g node] (contains? (:nodeset g) node)) (has-edge? [g n1 n2] (contains? (get-in g [:adj n1]) n2)) (out-degree [g node] (count (get-in g [:adj node]))) (out-edges [g] (partial out-edges g)) (out-edges [g node] (for [n2 (successors g node)] [node n2])) (successors [g] (partial successors g)) (successors [g node] (get-in g [:adj node])) EditableGraph (add-nodes* [g nodes] (reduce (fn [g node] (update-in g [:nodeset] conj node)) g nodes)) (add-edges* [g edges] (reduce (fn [g [n1 n2]] (-> g (update-in [:nodeset] conj n1 n2) (update-in [:adj n1] (fnil conj #{}) n2) (update-in [:adj n2] (fnil conj #{}) n1))) g edges)) (remove-nodes* [g nodes] (let [nbrs (mapcat (fn* [p1__66951#] (successors g p1__66951#)) nodes)] (-> g (update-in [:nodeset] (fn* [p1__66952#] (apply disj p1__66952# nodes))) (assoc :adj (remove-adj-nodes (:adj g) nodes nbrs disj))))) (remove-edges* [g edges] (reduce (fn [g [n1 n2]] (-> g (update-in [:adj n1] disj n2) (update-in [:adj n2] disj n1))) g edges)) (remove-all [g] (assoc g :nodeset #{} :adj {})))


(comment (macroexpand-1 '(extended-record BasicEditableGraph [nodeset adj]

Graph
Expand Down
Empty file modified src/loom/io.cljc
100644 → 100755
Empty file.
Empty file modified src/loom/label.cljc
100644 → 100755
Empty file.
Empty file modified test/loom/test/alg.clj
100644 → 100755
Empty file.
Empty file modified test/loom/test/alg_generic.clj
100644 → 100755
Empty file.
Empty file modified test/loom/test/attr.clj
100644 → 100755
Empty file.
Empty file modified test/loom/test/flow.clj
100644 → 100755
Empty file.
Empty file modified test/loom/test/graph.clj
100644 → 100755
Empty file.
Empty file modified test/loom/test/label.clj
100644 → 100755
Empty file.

0 comments on commit 4ded279

Please sign in to comment.