Skip to content

Instantly share code, notes, and snippets.

@didibus didibus/defnk.clj
Last active Nov 3, 2018

Embed
What would you like to do?
Clojure macro to define strict keyword argument functions
(defmacro defnk
[name & fdecl]
(let [doc-string? (when (string? (first fdecl))
(first fdecl))
fdecl (if (string? (first fdecl))
(next fdecl)
fdecl)
attr-map? (when (map? (first fdecl))
(first fdecl))
fdecl (if (map? (first fdecl))
(next fdecl)
fdecl)
params (first fdecl)
m (gensym)
params* [{:keys params :as m}]
fdecl (next fdecl)
prepost-map? (if (map? (first fdecl))
(first fdecl)
{})
fdecl (if (map? (first fdecl))
(next fdecl)
fdecl)
body fdecl]
`(defn ~name
~@(if doc-string? [doc-string?] [])
~@(if attr-map? [attr-map?] [])
~params*
~(merge-with (comp vec concat)
prepost-map?
{:pre [`(every? some? ~params)
`(= ~(count params) (count ~m))]})
~@body)))
@didibus

This comment has been minimized.

Copy link
Owner Author

didibus commented Nov 3, 2018

It is strict in that additional key arguments throw an error at call site, and missing keys throw an error at call site.

@didibus

This comment has been minimized.

Copy link
Owner Author

didibus commented Nov 3, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.