Skip to content

Commit

Permalink
Use seq API instead of explicit list API for traversal.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrupp committed Mar 23, 2013
1 parent c72d435 commit fd41386
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/graffy/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@

(defn- traverse [g n strategy]
(assert (#{:bf :df} strategy) "Strategy must be one of :bf (breadth-first) or :df (depth-first)")
(loop [acc [] stack (list n)]
(loop [acc [] stack (seq [n])]
(if (zero? (count stack))
acc
(let [nxt (peek stack)
rst (pop stack)]
(if (contains? (into #{} acc) nxt)
(recur acc rst)
(if (= strategy :df)
(recur (conj acc nxt) (apply list (concat (neighbors g nxt) rst)))
(recur (conj acc nxt) (apply list (concat rst (neighbors g nxt))))))))))
(if (some #{(first stack)} acc)
(recur acc (rest stack))
(if (= strategy :df)
(recur (conj acc (first stack)) (concat (neighbors g (first stack)) (rest stack)))
(recur (conj acc (first stack)) (concat (rest stack) (neighbors g (first stack)))))))))

(defn dft [g n]
(traverse g n :df))
Expand Down

0 comments on commit fd41386

Please sign in to comment.