Skip to content

Instantly share code, notes, and snippets.

@lfranchi
Created November 29, 2012 21:16
Show Gist options
  • Save lfranchi/4171974 to your computer and use it in GitHub Desktop.
Save lfranchi/4171974 to your computer and use it in GitHub Desktop.
(defn item<?
[{patha :path} {pathb :path}]
(loop [patha patha
pathb pathb]
(let [{l :branch l-disamb :disamb} (first patha)
{r :branch r-disamb :disamb} (first pathb)]
(cond
(nil? l) (= r 1)
(nil? r) (= l 0)
(= l r) (if (and (= 1 (count patha)) (= 1 (count pathb)))
(disamb<? l-disamb r-disamb) ;; Finishes with two mini-siblings, order by disambiguator
(recur (subvec patha 1) (subvec pathb 1))) ;; Normal case
:else (< l r)))))
(defn ancestor?
"Returns if the first path is an ancestor to the second"
[patha pathb]
(loop [patha patha ;; Otherwise, determine if it's an ancestor
pathb pathb]
(let [{l :branch l-disamb :disamb} (first patha)
{r :branch r-disamb :disamb} (first pathb)]
(cond
(nil? l) true
(= l r) (recur (subvec patha 1) (subvec pathb 1))) ;; Normal case, equal so continue down the tree
:else false)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment