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.2.1.180 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.179 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.178 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.177 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.176 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.175 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.174 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.173 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.172 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.171 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.170 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.169 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.167 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.166 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.165 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.164 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.163 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.162 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.161 tracks/clojure/exercises/pov/src/example.clj
trackler-2.2.1.160 tracks/clojure/exercises/pov/src/example.clj