Skip to content

Instantly share code, notes, and snippets.

@honza
Created May 7, 2014 17:06
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 honza/a6a7cd9511ceaca1687a to your computer and use it in GitHub Desktop.
Save honza/a6a7cd9511ceaca1687a to your computer and use it in GitHub Desktop.
Impure function detectio
;; If you name your impure functions with a name ending in "!",
;; you can use this handy macro to check if a form uses any impure functions.
(declare find-impure)
(defn ends-in-bang? [s]
(= \! (last (str s))))
(defn impure? [e]
(cond
(seq? e) (find-impure e)
(symbol? e) (when (ends-in-bang? e)
e)
:else nil))
(defn find-impure [coll]
(when-let [s (seq coll)]
(lazy-seq
(cons
(impure? (first s))
(find-impure (rest s))))))
(defmacro pure [e]
(let [i (find-impure e)
impures (set (filter identity (flatten i)))]
(when (seq impures)
(str "Impure functions detected: "
(apply str (interpose " - " impures))))))
;; Example
(pure (if (= 1 2)
(yiss! 1 2 3)
false))
;; => Impure functions detected: yiss!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment