Skip to content

Instantly share code, notes, and snippets.

@dustingetz
Created November 25, 2014 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustingetz/d0c07114e0d6ccad49ef to your computer and use it in GitHub Desktop.
Save dustingetz/d0c07114e0d6ccad49ef to your computer and use it in GitHub Desktop.
(ns clojure-examples.tree)
(defn array? [thing]
(.isArray (class thing)))
(defn map-tree [t f path state]
(cond
(map? t)
(reduce
(fn [acc [k v]]
(assoc acc k (map-tree v f (conj path k) state)))
{}
t)
;; is it it a seq, vec or array
(or
(seq? t)
(vector? t)
(array? t))
(vec
(map-indexed
(fn [idx elt]
(map-tree elt f (conj path idx) state))
t))
:at-leaf-node
(f state t path)))
(comment
(def global :me)
(map-tree [[1 2 3] [4 5 6]
{:a 77 :b [5 5 5]
:c {:e 2}}]
(fn [state elt path]
(printf "elt=%s path=%s state=%s\n" elt path state)
(inc elt))
[]
{:my :state})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment