-
-
Save Sophia-Gold/d48be3ec125a453de7cec2033a465ef8 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 walk-ast | |
(:require [com.rpl.specter :refer :all] | |
[clojure.tools.analyzer.jvm :refer [analyze]] | |
[fipp.edn :refer [pprint] :rename {pprint fipp}])) | |
(def map-key-walker | |
(recursive-path | |
[akey] p | |
(cond-path map? [ALL (if-path [FIRST #(= % akey)] | |
LAST | |
[LAST p])] | |
vector? [ALL (if-path map? | |
[ALL (if-path [FIRST #(= % akey)] | |
LAST | |
[LAST p])])]))) | |
(defn select-locals [ast] (select (map-key-walker :locals) ast)) | |
(def MAP-NODES | |
(recursive-path | |
[] p | |
(cond-path map? (continue-then-stay MAP-VALS p) | |
coll? [ALL p]))) | |
(defn keep-locals [ast] (transform MAP-NODES #(select-keys % [:locals]) ast)) | |
(def data (analyze '(fn [x y] (fn [a b] (+ x a))))) | |
(def unstructured (select-locals data)) | |
(def structured (keep-locals data)) ;; => {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment