Last active
August 29, 2015 14:17
-
-
Save cshepp/43cb2c600058beba68a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn find-cycle | |
"finds a single cycle in the parents | |
starting at the seed index" | |
[a b seed] | |
(loop [coll (mapv vector a b) | |
cyl #{} | |
cur (get-in coll [seed 0])] | |
(let [idx (first (keep-indexed #(if (= (first %2) cur) %1) coll))] | |
(if (nil? idx) | |
(vec cyl) | |
(recur (vec-remove coll idx) | |
(into cyl (nth coll idx)) | |
(last (nth coll idx))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment