pulling out malli schemas for defn
s (Vars):
(-var-schema #'+)
;[:function
; [:=> :catn :any]
; [:=> [:catn [x :any]] :any]
; [:=> [:catn [x :any] [y :any]] :any]
; [:=> [:catn [x :any] [y :any] [more [:+ :any]]] :any]]
(-var-schema #'println)
; [:=> [:catn [more [:* :any]]] :any]
Is this valid syntax?
[:=> :catn :any]
yes, vectors are optional if there are no childs or props, e.g. [:int]
can be written as :int
.
same for :catn
. “empty sequence with named children”
the imp btw:
(defn -var-schema [var]
(let [-=> (fn [as] (let [[f s [t]] (partition-by #{'&} as)
[fas ras rop] (cond t [f t :+], s [nil (first s) :*], :else [f])]
[:=> (if (or (seq fas) ras)
(vec (concat [:catn] (mapv (fn [a] [a :any]) fas) (when ras [[ras [rop :any]]])))
:catn) :any]))
{:keys [arglists]} (meta var)
-=>s (mapv -=> arglists)]
(if (second arglists) (into [:function] -=>s) (first -=>s))))
not sure if it’s a good idea to use :arglists
var meta, but, works on my repl just now at least 🙂
not a bad idea
some Clojure vars use something like x*
which denotes multiple xs, so the arglist is not always reliable, but in most cases it's auto-generated from the defn itself