Skip to content

Instantly share code, notes, and snippets.

@duncanmak
Created October 20, 2010 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duncanmak/637492 to your computer and use it in GitHub Desktop.
Save duncanmak/637492 to your computer and use it in GitHub Desktop.
(defn conj-entry [trails entry transform]
(let [all-trails (sort-by #(trail-proximity % entry) trails)]
(if (empty? all-trails)
[(make-trail entry)]
(loop [[trail & trails] all-trails
results (transient [])]
(cond
;; no matches found after scanning all trails
(nil? trail)
(do
(when *debug* (print "!"))
(conj (persistent! results) (make-trail entry)))
;; trail matches
(trail-matches? trail (transform-entry transform entry))
(let [new-trail (add-entry trail entry)]
(when *debug* (print "+"))
(apply conj (persistent! results) new-trail trails))
:else ;; try the next trail
(recur trails
(conj! results trail)))))))
(defn propagate-iterative [entries transform relevant-trails other-trails]
(loop [entries entries
trails relevant-trails]
(if (empty? entries)
(do (when *debug* (println))
(if (empty? other-trails)
trails
(apply conj trails other-trails)))
(let [entry (first entries)]
(recur (rest entries)
(conj-entry trails entry transform))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment