Created
March 30, 2015 05:35
-
-
Save timsgardner/4161b4bb5b75e3c78a0d to your computer and use it in GitHub Desktop.
meval
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
;; ============================================================ | |
;; why didn't I think of this earlier | |
;; strictly speaking this uses eval, so maybe problematic 4 certain | |
;; export targets, depending on how we handle that; eval only called | |
;; at compile time tho, so maybe we could excise it in a way that's | |
;; smart enough to still allow this sort of tomfoolery. | |
(defmacro meval [& stuff] | |
(eval (cons 'do stuff))) | |
(defmacro defn-meval [& preamble-and-meval-to-forms] | |
(let [preamble (butlast preamble-and-meval-to-forms) | |
meval-to-forms (last preamble-and-meval-to-forms) | |
mevaluated (eval meval-to-forms)] | |
`(defn ~@preamble | |
~@mevaluated))) | |
;; ============================================================ | |
;; usage | |
(meval | |
(cons 'do | |
(for [n (range 10) | |
:let [args (repeatedly n gensym)]] | |
`(defn ~(symbol (str "some-function-" n)) [~@args] | |
~(cons 'println (butlast (interleave args (repeat ", ")))))))) | |
;;=> (some-function-3 :a :b :c) | |
;; :a , :b , :c | |
;; nil | |
(defn-meval multiarg-lunacy | |
(for [n (range 20) | |
:let [args (repeatedly n gensym)]] | |
`([~@args] ~(cons 'println (butlast (interleave args (repeat ", "))))))) | |
;;=> (multiarg-lunacy :a :b :c) | |
;; :a , :b , :c | |
;; nil | |
;; these are boring examples, I'm sure there's plenty more exciting | |
;; ones to be explored | |
use this all the time with absolute abandon to make awesome code quickly and a glorious throbbing mess
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also need to test how this composes etc