Created
May 7, 2014 17:06
-
-
Save honza/a6a7cd9511ceaca1687a to your computer and use it in GitHub Desktop.
Impure function detectio
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
;; 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