Skip to content

Instantly share code, notes, and snippets.

@timsgardner
Created March 30, 2015 05:35
Show Gist options
  • Save timsgardner/4161b4bb5b75e3c78a0d to your computer and use it in GitHub Desktop.
Save timsgardner/4161b4bb5b75e3c78a0d to your computer and use it in GitHub Desktop.
meval
;; ============================================================
;; 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
@timsgardner
Copy link
Author

Also need to test how this composes etc

@timsgardner
Copy link
Author

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