Consultancy - Research Lab - Incubator

based in beautiful Zürich, Switzerland

- 537 words - 3 min read

TL;DR I was looking for a prototype of a Clojure macro that fulfills three requirements: (1) Generates a function. (2) Takes options as parameters and provides those to the generated function. (3) And lastly the generated function takes arguments. Spoiler! This is it...

(defmacro defsample [name & args]
  (let [options (apply hash-map args)]
    `(defn ~name [input#]
       (prn (str (:text ~options) " " input# ".")))))

(defsample sample :text "Hello")

(sample "world")

Keep on reading for a detailed description. In my quest to come up with the prototype above I started with the most basic version...