rlm@0: (ns coderloop.reverse-seq rlm@0: (:use [clojure.contrib rlm@0: [duck-streams :only [file-str read-lines]] rlm@0: [str-utils :only [re-split]]]) rlm@0: (:use rlm.shell-inspect) rlm@0: (:use [clojure [string :only [trim]]])) rlm@0: rlm@0: (def c (file-str "/home/r/coderloop-test/reversesequence-c.in")) rlm@0: (def a (file-str "/home/r/coderloop-test/reversesequence-a.in")) rlm@0: (def b (file-str "/home/r/coderloop-test/reversesequence-b.in")) rlm@0: rlm@0: (defn test-trace-map [] rlm@0: (= [:a :b :c :d :e] rlm@0: (trace-map {:a :b :d :e :c :d :b :c}))) rlm@0: rlm@0: rlm@0: (defn read-name-map rlm@0: "takes a file of pairs of names and returns a map from the first rlm@0: element in those pairs to the second element in the pairs." rlm@0: [#^java.io.File file] rlm@0: (let [pairs (map #(re-split #"\W+" (trim %)) (read-lines file))] rlm@0: (zipmap (map first pairs) (map second pairs)))) rlm@0: rlm@0: (defn start-linked-map rlm@0: "takes a map-representing-a-list and finds the beginning of that list" rlm@0: [m] rlm@0: (first (remove (set (vals m)) (keys m)))) rlm@0: rlm@0: (defn trace-map rlm@0: "chains through a map-representing-a-list, building a list along the way." rlm@0: [m] rlm@0: (loop [index (start-linked-map m) rlm@0: chain (vector index)] rlm@0: (let [new-index (get m index ::sentinel)] rlm@0: (if (= new-index ::sentinel) rlm@0: chain rlm@0: (recur new-index (conj chain new-index)))))) rlm@0: rlm@0: rlm@0: rlm@0: (if (command-line?) rlm@0: (dorun rlm@0: (map println (reverse (trace-map (read-name-map (file-str (first *command-line-args*))))))))