Skip to content

Instantly share code, notes, and snippets.

@tyano
Created January 14, 2023 09:58
Show Gist options
  • Save tyano/017c3577181208c571fe54455a771aa8 to your computer and use it in GitHub Desktop.
Save tyano/017c3577181208c571fe54455a771aa8 to your computer and use it in GitHub Desktop.
(defmacro defdelegation
[protocol-name options & body]
(let [{:keys [prefix] :or {prefix "-"}} (if (map? options) options {})
body (if (map? options) body (cons options body))
protocol-body (mapv (fn [definition]
(let [fn-sym (first definition)
fn-body (rest definition)]
(apply list (symbol (str prefix (name fn-sym))) fn-body)))
body)
fn-defs (mapv (fn [definition]
(let [fn-sym (first definition)
protocol-fn-sym (symbol (str prefix (name fn-sym)))
fn-defs (rest definition)
maybe-doc (last fn-defs)
fn-defs (if (string? maybe-doc) (drop-last fn-defs) fn-defs)
document (if (string? maybe-doc) maybe-doc "")
fn-body (mapv (fn [arg-list]
(list arg-list (apply list protocol-fn-sym arg-list)))
fn-defs)]
(apply list `defn fn-sym document fn-body)))
body)]
`(do
(defprotocol ~protocol-name ~@protocol-body)
~@fn-defs)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment