Last active
October 29, 2020 02:23
-
-
Save borkdude/a391146ad81a06c28fb97ccdc1f64d44 to your computer and use it in GitHub Desktop.
Spec grep: find usages or reify in clojure.core with two or more interfaces. This idea has been implemented in a library now: https://github.com/borkdude/grasp
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
;; see https://github.com/borkdude/grasp for a more elaborate implementation | |
(ns script | |
(:require [clojure.java.io :as io] | |
[clojure.pprint :refer [pprint]] | |
[edamame.core :as e])) | |
(def clojure-core (slurp (io/resource "clojure/core.clj"))) | |
(def parsed (e/parse-string-all clojure-core | |
{:all true :auto-resolve '{:current clojure.core}})) | |
(require '[clojure.spec.alpha :as s]) | |
(s/def ::clause (s/cat :sym symbol? :lists (s/+ list?))) | |
;; find usages of reify with at least two interfaces | |
(s/def ::pattern | |
(s/cat :reify #{'reify} | |
:clauses (s/cat :clause ::clause :clauses (s/+ ::clause)))) | |
(defn matches | |
[tree spec] | |
(->> tree | |
(tree-seq seqable? seq) | |
(filter #(s/valid? spec %)))) | |
(doseq [m (matches parsed ::pattern)] | |
(prn (meta m)) | |
(pprint m) | |
(println)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: