Skip to content

Instantly share code, notes, and snippets.

@moea
Created October 14, 2020 15:35
Show Gist options
  • Save moea/5f508621e344666070cc3c890b798227 to your computer and use it in GitHub Desktop.
Save moea/5f508621e344666070cc3c890b798227 to your computer and use it in GitHub Desktop.
(ns replicate.tree
[clojure.walk
:refer [postwalk]]))
(defprotocol NodeStore
(persist [store node])
(fetch [store node-hash]))
(defprotocol Node
(insert [node k store])
(search [node k store]))
(extend-protocol Node
nil
(insert [_ k _] [k nil nil])
(search [_ _ _] nil)
#?(:clj clojure.lang.IPersistentVector
:cljs PersistentVector)
(insert [[k :as node] k' store]
(cond (< k' k) (update node 1 insert k' store)
(> k' k) (update node 2 insert k' store)
:else node))
(search [[k l r :as node] k' store]
(cond (< k' k) (search l k' store)
(> k' k) (search r k' store)
:else node))
#?(:clj java.lang.String
:cljs string)
(insert [n-hash k store]
(-> (fetch store n-hash) (insert k store)))
(search [n-hash k store]
(-> (fetch store n-hash) (search k store))))
(defn persist-tree [node store]
(postwalk
#(cond->> % (vector? %) (persist store))
node))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment