Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up ns and make code more idiomatic #62

Merged
merged 2 commits into from
Jun 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/loom/alg.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ can use these functions."
:author "Justin Kramer"}
loom.alg
(:require [loom.alg-generic :as gen]
[loom.flow :as flow])
(:require [loom.graph
[loom.flow :as flow]
[loom.graph
:refer [add-nodes add-edges nodes edges successors weight predecessors
out-degree in-degree weighted? directed? graph transpose]
:as graph]
[loom.alg-generic :refer [trace-path preds->span]])
(:require [clojure.data.priority-map :as pm]
[loom.alg-generic :refer [trace-path preds->span]]
[clojure.data.priority-map :as pm]
[clojure.set :as clj.set]))

;;;
Expand Down Expand Up @@ -311,7 +311,7 @@ can use these functions."
false
(let [dist (if (weighted? g)
(weight g)
(fn [u v] (if (graph/has-edge? g u v) 1 nil)))]
(fn [u v] (when (graph/has-edge? g u v) 1)))]
(reduce (fn [acc node]
(assoc acc node (gen/dijkstra-span (successors g) dist node)))
{}
Expand Down Expand Up @@ -434,7 +434,7 @@ can use these functions."
;; TODO: could be better
(if (some #(and (coloring %) (= (coloring v) (coloring %)))
nbrs)
nil ;not bipartite
nil ; graph is not bipartite
(let [nbrs (remove coloring nbrs)]
(recur (into coloring (for [nbr nbrs] [nbr color]))
(into (pop queue) nbrs))))))))]
Expand Down Expand Up @@ -474,7 +474,7 @@ can use these functions."
successors
(concat successors
(graph/predecessors g node)))]
(set (filter (complement nil?)
(set (remove nil?
(map #(get coloring %)
neighbors)))))

Expand Down Expand Up @@ -621,7 +621,7 @@ can use these functions."
curr-dist ((second (peek q)) 2)
;; update path
explored (assoc explored curr-node ((second (peek q)) 1))
nbrs (filter (complement explored) (successors g curr-node))
nbrs (remove explored (successors g curr-node))
;; we do this for following reasons
;; a. avoiding duplicate heuristics computation
;; b. duplicate entries for nodes, which needs to be removed later
Expand Down
7 changes: 3 additions & 4 deletions src/loom/alg_generic.clj
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
seen)))))
(when-let [parent (peek stack)]
(recur successors parent (peek nbrstack)
(pop stack) (pop nbrstack) (conj seen start)))))]
(pop stack) (pop nbrstack) (conj seen start)))))]
(when-not (seen start)
(step successors start (successors start) [] [] (conj seen start)))))

Expand Down Expand Up @@ -133,9 +133,8 @@
once for each direction."
[successors start & {:keys [seen return-seen] :or {seen #{}}}]
(if (seen start)
(if return-seen
[nil seen]
nil)
(when return-seen
[nil seen])
(loop [start start
nbrs (successors start)
stack []
Expand Down
4 changes: 2 additions & 2 deletions src/loom/flow.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns ^{:doc "Algorithms for solving network flow"
:author "Robert Lachlan"}
loom.flow
(:require [loom.alg-generic :as gen :only [bf-path]]))
(:require [loom.alg-generic :as gen :refer [bf-path]]))


(defn residual-capacity
Expand Down Expand Up @@ -74,7 +74,7 @@
[successors predecessors capacity flow s t]
(gen/bf-path
(fn [vertex]
(distinct (filter #(> (residual-capacity capacity flow vertex %) 0)
(distinct (filter #(pos? (residual-capacity capacity flow vertex %))
(concat (successors vertex) (predecessors vertex)))))
s t))

Expand Down
8 changes: 4 additions & 4 deletions src/loom/graph.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ on adjacency lists."
(extend-type clojure.lang.IPersistentVector
Edge
(src [edge] (get edge 0))
(dest [edge] (get edge 1)))
(dest [edge] (get edge 1)))

; Default implementation for maps
(extend-type clojure.lang.IPersistentMap
Expand Down Expand Up @@ -108,7 +108,7 @@ on adjacency lists."
(contains? (get-in g [:adj n1]) n2))
:out-degree (fn [g node]
(count (get-in g [:adj node])))
:out-edges (fn
:out-edges (fn
([g] (partial out-edges g))
([g node] (for [n2 (successors g node)] [node n2])))}

Expand Down Expand Up @@ -144,7 +144,7 @@ on adjacency lists."
([g node] (get-in g [:in node])))
:in-degree (fn [g node]
(count (get-in g [:in node])))
:in-edges (fn
:in-edges (fn
([g] (partial in-edges g))
([g node] (for [n2 (predecessors g node)] [n2 node])))})

Expand Down Expand Up @@ -430,7 +430,7 @@ on adjacency lists."
(defn subgraph
"Return a graph without all but the given nodes"
[g ns]
(remove-nodes* g (filter (complement (set ns)) (nodes g))))
(remove-nodes* g (remove (set ns) (nodes g))))

(defn add-path
"Add a path of edges connecting the given nodes in order"
Expand Down
15 changes: 7 additions & 8 deletions src/loom/io.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
(let [d? (directed? g)
w? (weighted? g)
a? (attr? g)
node-label (if node-label node-label
node-label (or node-label
(if a?
#(attr g % :label)
(constantly nil)))
edge-label (if edge-label edge-label
edge-label (or edge-label
(cond
a? #(if-let [a (attr g %1 %2 :label)]
a
Expand Down Expand Up @@ -115,12 +115,11 @@
;; There's an 'open' method in java.awt.Desktop but it hangs on Windows
;; using Clojure Box and turns the process into a GUI process on Max OS X.
;; Maybe it's ok for Linux?
(do
(condp = (os)
:mac (sh "open" (str f))
:win (sh "cmd" (str "/c start " (-> f .toURI .toURL str)))
:unix (sh "xdg-open" (str f)))
nil)))
(condp = (os)
:mac (sh "open" (str f))
:win (sh "cmd" (str "/c start " (-> f .toURI .toURL str)))
:unix (sh "xdg-open" (str f)))
nil))

(defn- open-data
"Write the given data (string or bytes) to a temporary file with the
Expand Down