Sha256: 56df6f079090edfda51cca4f2188baf94128148565df75c7403501879249fbca

Contents?: true

Size: 1.13 KB

Versions: 396

Compression:

Stored size: 1.13 KB

Contents

(ns pov
  (:require [clojure.zip :as zip]))

(defn- remove-child
  "Remove a child from a node"
  [parent child]
  (filter #(not= child %) parent))

(defn- reparent
  "Take a node, and make it the parent of its parent"
  [loc]
  (let [current (zip/node loc)
        parent (zip/up loc)
        belonged-to (when parent
                      (-> parent
                          (zip/edit remove-child current)
                          reparent
                          list))]
    (vec (concat current belonged-to))))

(defn- find-node
  "Find the node whose identifier matches the given value"
  [v tree]
  (loop [loc (-> tree zip/vector-zip zip/next)]
    (cond (= v (zip/node loc)) (zip/up loc)
          (zip/end? loc) nil
          :else (recur (zip/next loc)))))

(defn of
  "Find a node by its identifier and raise to the root"
  [s tree]
  (when-let [loc (find-node s tree)]
    (reparent loc)))

(defn path-from-to
  "Find a path from src node to dest node"
  [src dest graph]
  (when-let [loc (find-node dest (of src graph))]
    (let [dest-node (zip/node loc)
          path (concat (zip/path loc) [dest-node])]
      (map first path))))

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.0.6.40 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.39 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.38 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.37 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.36 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.35 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.34 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.33 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.32 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.31 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.30 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.29 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.28 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.27 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.26 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.25 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.24 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.23 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.22 tracks/clojure/exercises/pov/src/example.clj
trackler-2.0.6.21 tracks/clojure/exercises/pov/src/example.clj