Created
April 1, 2014 04:13
-
-
Save mavant/9907564 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(use '[clojure.string :as s]) | |
(defn remove-whitespace "Given a string representing some Clojure code as input, remove superfluous whitespace." [c] (s/replace c #"\s+" " ")) | |
(defn parens->indents "Given a list of lines in which each begins an expression, prepends a newline and the appropriate number of tabs to each line." [c] (map (fn [a b] (str "\n" (apply str (repeat a "\t")) b)) (indent-levels (match-parens c)) (split-on-all-parens c))) | |
(defn remove-empty-lines "Given a list of strings, filters out any string which contains no word characters." [l] (filter (fn [c] (< 0 (count (re-seq #"\w" c)))) l)) | |
(defn indent-levels "Given a list of lines of code, determine the appropriate number of tabs for each line." [l] (reduce (fn [a b] (conj a (+ (peek a) (if (= b ")") -1 1)))) [-1] l)) | |
(defn match-parens "Given a string of code, return a list of all the parentheses in the code, in the order in which they appear." [c] (re-seq #"[\)\(]" c)) | |
(defn split-on-all-parens "Given a string of code, return a list split into parts by the locations of parentheses." [c] (s/split c #"[\)\(]")) | |
(defn pythonize "Takes a string of Clojure code, and converts parentheses to tabs and newlines for clarity of reading." [c] (-> c remove-whitespace parens->indents remove-empty-lines (partial apply str))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment