Created
November 25, 2014 04:15
-
-
Save dustingetz/d0c07114e0d6ccad49ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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