From fd413869a187bb608a2d78d9a68b2e40f77058ca Mon Sep 17 00:00:00 2001 From: David Rupp Date: Sat, 23 Mar 2013 15:15:41 -0700 Subject: [PATCH] Use seq API instead of explicit list API for traversal. --- src/graffy/core.clj | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/graffy/core.clj b/src/graffy/core.clj index 6d81058..fcbd014 100644 --- a/src/graffy/core.clj +++ b/src/graffy/core.clj @@ -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))