malli

https://github.com/metosin/malli :malli:
ikitommi 2021-03-06T20:12:29.200300Z

pulling out malli schemas for defns (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]

borkdude 2021-03-06T20:17:36.200700Z

Is this valid syntax?

[:=> :catn :any]

ikitommi 2021-03-06T20:19:32.202Z

yes, vectors are optional if there are no childs or props, e.g. [:int] can be written as :int.

ikitommi 2021-03-06T20:20:01.202600Z

same for :catn. “empty sequence with named children”

ikitommi 2021-03-06T20:20:18.202900Z

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))))

👍 1
ikitommi 2021-03-06T20:21:36.203800Z

not sure if it’s a good idea to use :arglistsvar meta, but, works on my repl just now at least 🙂

borkdude 2021-03-06T20:28:43.204Z

not a bad idea

borkdude 2021-03-06T20:29:14.204500Z

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